iconicCreator Posted September 15, 2009 Share Posted September 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174375-displaying-image-with-php-in-sequence/ Share on other sites More sharing options...
MadTechie Posted September 15, 2009 Share Posted September 15, 2009 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']; Quote Link to comment https://forums.phpfreaks.com/topic/174375-displaying-image-with-php-in-sequence/#findComment-919190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.