Jump to content

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'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.