Image rotation on refresh.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jeefo
Forum Newbie
Posts: 16
Joined: Thu Jun 16, 2005 3:38 pm
Location: Tokyo, Japan

Image rotation on refresh.

Post by Jeefo »

Are there any PHP scripts that will change an image when the page is refreshed (or the page is loaded again)? Nothing fancy, I just need a simple PHP code. Thanks for your help.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

How are all your images stored?
In a database? Stored in a folder? -- or a combination of both.
Do you want the image to be randomly chosen, or in sequence?
User avatar
Jeefo
Forum Newbie
Posts: 16
Joined: Thu Jun 16, 2005 3:38 pm
Location: Tokyo, Japan

Post by Jeefo »

They'll be stored in a folder, but does it matter when the images and the script are in the same directory? Chosen randomly is fine.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Something like:

Code: Select all

$dir_of_images = '/path/to/directory/'; //Keep trailing slash
$handle = opendir($dir_of_images);
$images = array();
while ($file = readdir($handle)) { //Loop over the files
    $ext = substr($file, -3);
    if ($ext == 'jpg' || $ext = 'gif') { //Add any other 3 letter extensions here
        $images[] = $file; //Store image in array
    }
}
shuffle($images); //Shake them all up 
echo '<img src="'.$dir_of_images.$images[0].'" alt="" />'; //Output the image
User avatar
Jeefo
Forum Newbie
Posts: 16
Joined: Thu Jun 16, 2005 3:38 pm
Location: Tokyo, Japan

Post by Jeefo »

d11wtq wrote:Something like:
Thanks, but even though I specified the directory of the images, they don't show up at all. Also, I need the images to be clickable.
Post Reply