Jump to content

Displaying image with PHP in Sequence.


iconicCreator

Recommended Posts

Good Afternoon everyone!

Hope all is going well.

 

I have created a PHP script that changes images randomly with a caption for each image every time the browser is refreshed.

 

I would like to modified this script so that the images change in sequence when the browser is refreshed.

 

Lets say I have four images. The first image will be selected randomly and displayed. This will insure the user sees a different image every time they visit the page.

 

The rest of the images will display in sequence based on how they are assign to the script when the page is revisited or refreshed.

 

So if I had 50 images, the first image users will see will be selected randomly then the rest in sequence and the whole thing starts over when the browser is refresh or the page revisited.

 

Here is what I already have but is currently random.

 

<?php 
$images = array(
array('file'    => 'image1',
        'caption' => 'A day at the beach'),


array('file'    => 'image2',
        'caption' => 'Apples and Peaches'),


array('file'    => 'image3',
        'caption' => 'A beautiful night'),

array('file'    => 'image4',
        'caption' => 'Golden Harp'));


$i = rand(0, count($images)-1);
$selectedImage = "../my_images/{$images[$i]['file']}.jpg";
$caption = $images[$i]['caption'];

?>

 

 

Images display in page where this is placed.

 

<div id="random_image_display">
<img src="<?php echo $selectedImage; ?>" alt="Random Images" />
</div>

<div id="caption_box">
<?php echo($caption); ?>
</div>

 

 

Thanks very much everyone!

 

IC

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/174375-displaying-image-with-php-in-sequence/
Share on other sites

You could use cookies,

 

heres a basic example (i tried to keep it simple) i hope you get the idea

replace

$i = rand(0, count($images)-1);

with

if(!empty($_COOKIE['ad_image']))
{
  $_COOKIE['ad_image']++;
  if($_COOKIE['ad_image'] > count($images)) $_COOKIE['ad_image']=0;
}else{
  $_COOKIE['ad_image'] = 0;
}

$i = $_COOKIE['ad_image'];

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.