Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Over the past year I have found your site to be EXCELLENT. Never have I been able to find so many answers to such vast problems and it is an excellent service..."

Geography

Where in the world do Tek-Tips members come from?

display 3 different images from array

Brianfree (Programmer)
7 May 12 6:32
Hi, i have found the following code which works fine for two images but i am having problems adding adding more data to the array and displaying a third different image.

Please can someone tweak the code below to help me?

Kindest regards,

Brian

<?php
$images = array(0 => array ('portfolio/index.php','Qlink.gif'), 1 => array ('news.php','Qlink2.gif'), 2 => array ('fountain.php','Qlink3.gif'), 3 => array ('castle.php','Qlink4.gif'));
$imageone='';
$imagetwo='';

function create(){
$chosen = array();
global $imageone, $imagetwo;
$i='1';
while ($i <= 2){
$random = rand(0, 3);
if (!in_array($random, $chosen)){
array_push($chosen, $random);
$i++;
}
}
$imageone = $chosen['0'];
$imagetwo = $chosen['1'];
}
create();
?>


Then call it like this


<a href="<?php echo $images[$imageone][0]; ?>" title="link to page"><img src="default_image_folder/<?php echo $images[$imageone][1]; ?>" alt="random image and link" /></a>
 
feherke (Programmer)
7 May 12 7:08
Hi

I would prefer to use the array_rand() function :

CODE --> PHP ( fragment )

function create()
{
  global $images;
  $chosen = array_rand($images, 2);
  shuffle($chosen);
  return $chosen;
}

list($imageone, $imagetwo) = create();
Actually that is more useful with associative arrays :

CODE --> PHP

<?php
$images = array(
  'portfolio/index.php' => 'Qlink.gif',
  'news.php' => 'Qlink2.gif',
  'fountain.php' => 'Qlink3.gif',
  'castle.php' => 'Qlink4.gif'
);

function create()
{
  global $images;
  $chosen = array_rand($images, 2);
  shuffle($chosen);
  return $chosen;
}

list($imageone, $imagetwo) = create();
?>

<!-- then use it -->

<a href="<?php echo $imageone; ?>" title="link to page"><img src="default_image_folder/<?php echo $images[$imageone]; ?>" alt="random image and link" /></a>
However I see no reason why those dumb $imageone and $imagetwo variables are used.
 

Feherke.
http://feherke.github.com/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close