Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display 3 different images from array

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
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>
 
Hi

I would prefer to use the [tt]array_rand()[/tt] function :
Code:
[b]function[/b] [COLOR=darkgoldenrod]create[/color][teal]()[/teal]
[teal]{[/teal]
  [b]global[/b] [navy]$images[/navy][teal];[/teal]
  [navy]$chosen[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]array_rand[/color][teal]([/teal][navy]$images[/navy][teal],[/teal] [purple]2[/purple][teal]);[/teal]
  [COLOR=darkgoldenrod]shuffle[/color][teal]([/teal][navy]$chosen[/navy][teal]);[/teal]
  [b]return[/b] [navy]$chosen[/navy][teal];[/teal]
[teal]}[/teal]

[b]list[/b][teal]([/teal][navy]$imageone[/navy][teal],[/teal] [navy]$imagetwo[/navy][teal])[/teal] [teal]=[/teal] [COLOR=darkgoldenrod]create[/color][teal]();[/teal]
Actually that is more useful with associative arrays :
PHP:
[teal]<?php[/teal]
[navy]$images[/navy] [teal]=[/teal] [b]array[/b][teal]([/teal]
  [green][i]'portfolio/index.php'[/i][/green] [teal]=>[/teal] [green][i]'Qlink.gif'[/i][/green][teal],[/teal]
  [green][i]'news.php'[/i][/green] [teal]=>[/teal] [green][i]'Qlink2.gif'[/i][/green][teal],[/teal]
  [green][i]'fountain.php'[/i][/green] [teal]=>[/teal] [green][i]'Qlink3.gif'[/i][/green][teal],[/teal]
  [green][i]'castle.php'[/i][/green] [teal]=>[/teal] [green][i]'Qlink4.gif'[/i][/green]
[teal]);[/teal]

[b]function[/b] [COLOR=darkgoldenrod]create[/color][teal]()[/teal]
[teal]{[/teal]
  [b]global[/b] [navy]$images[/navy][teal];[/teal]
  [navy]$chosen[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]array_rand[/color][teal]([/teal][navy]$images[/navy][teal],[/teal] [purple]2[/purple][teal]);[/teal]
  [COLOR=darkgoldenrod]shuffle[/color][teal]([/teal][navy]$chosen[/navy][teal]);[/teal]
  [b]return[/b] [navy]$chosen[/navy][teal];[/teal]
[teal]}[/teal]

[b]list[/b][teal]([/teal][navy]$imageone[/navy][teal],[/teal] [navy]$imagetwo[/navy][teal])[/teal] [teal]=[/teal] [COLOR=darkgoldenrod]create[/color][teal]();[/teal]
[teal]?>[/teal]

[gray]<!-- then use it -->[/gray]

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


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top