Jump to content

Rotate images at timed intervals


snorky

Recommended Posts

How do I rotate images (or banners...)  every n seconds?

 

I use the following code to rotate the images every time the page is loaded. However, I want to rotate the images every n seconds while the page is displayed.

<?php
/*	
---------------------------------------------------------------------------------	
find all files (in this case images) in a folder 
and rotate randomly when page is loaded or refreshed 
note: as written here, all files must be valid images and there can be no sub-folders
---------------------------------------------------------------------------------
*/

$filenum=0;								// initialize counter
$pix=array();							// create array
$usedir="../images/const/";				// show location of images to rotate
if ($handle = opendir($usedir))			// open folder for reading
{
    while (false !== ($file = readdir($handle))) 
 {
        if ($file != "." && $file != "..") 
	  {
            $file=$file++;				// increment the counter
			$pix[]=$file;			// select the next element from the array
        }
    }
    closedir($handle);
}

shuffle($pix);							// shuffle the elements of the array
$displ1=$pix[1];						// create var to id image to display
$displ2=$pix[2];						// damphyno why I did this
print									// display the randomly-selected image

"<img src='" . $usedir.$displ1 . "'><br />\n";  
?>

 

To see the code in action:

[*]http://www.maryellenoconnor.com/docs/silks.php

[*]Refresh the page, then repeat, repeat ....

Link to comment
https://forums.phpfreaks.com/topic/175814-rotate-images-at-timed-intervals/
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.