Jump to content

Random image order


abqslobra

Recommended Posts

Hello,

 

I'm trying to display 5 different images on one HTML page (in one column) such that each time the page is refreshed the order the images are displayed in will change, but all images are displayed every page load and each image has a URL associated with it. I've gotten close with this...

 

<?php
// rotate images randomly but w/o dups on same page - format:
// <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1'
// for second, etc
// (c) 2004 David Pankhurst - use freely, but please leave in my credit
$images=array( // list of files to rotate - add as needed
"images/1.jpg",
"images/2.jpg",
"images/3.jpg",
"images/4.jpg",
"images/5.jpg", );
$total=count($images);
$secondsFixed=10; // seconds to keep list the same
$seedValue=(int)(time()/$secondsFixed);
srand($seedValue);
for ($i=0;$i<$total;++$i) // shuffle list 'randomly'
{
$r=rand(0,$total-1);
$temp =$images[$i];
$images[$i]=$images[$r];
$images[$r]=$temp;
}
$index=(int)($_GET['i']); // image index passed in
$i=$index%$total; // make sure index always in bounds
$file=$images[$i];
header("Location: $file"); // and pass file reference back
?>

 

<html>
<img src='images/rotate.php?i=0'>
<img src='images/rotate.php?i=1'>
<img src='images/rotate.php?i=2'>
<img src='images/rotate.php?i=3'>
<img src='images/rotate.php?i=4'>
</html>

 

...but the only thing I can't figure out is how to associate URLs with the images. I tried an href around the img src but obviously that just applied a specific URL to whichever picture was called that time around. Can someone point me in the right direction?  ??? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/165879-random-image-order/
Share on other sites

How about something like this?

 

<?php
define('LINK_URL', 0);
define('IMAGE_URL', 1);

$images = array(
   array(LINK_URL => 'http://www.example1.com/', IMAGE_URL => 'images/someimage1.jpg'),
   array(LINK_URL => 'http://www.example2.com/', IMAGE_URL => 'images/someimage2.jpg'),
   array(LINK_URL => 'http://www.example3.com/', IMAGE_URL => 'images/someimage3.jpg'),
);

shuffle($images);

foreach($images as $image) {
   echo '<a href="' . $image[LINK_URL] . '"><img src="' . $image[iMAGE_URL] . '" /></a>';
}
?>

 

If the image and link locations vary only in numbers though (eg, they count up from 1 to 20) it would be easier to just use range to define the array before you shuffle it. That way you don't have to deal with hard-coding each individual image link and its url, only the minimum image number and maximum image number.

 

edit: clarity.

Link to comment
https://forums.phpfreaks.com/topic/165879-random-image-order/#findComment-874979
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.