A Simple PHP image rotator
This has probably been done a thousand times over by many programmers, but I thought I’d offer just one more to the collection.
Three Simple Steps
PHP: Function randomImage
<?php
function randomImage ( $array ) {
$total = count($array);
$call = rand(0,$total-1);
return $array[$call];
}
?>
The Image Array
<?php
$my_images = array (
"dustin.jpg",
"Jeffrey.jpeg",
"Chrissy.png",
"Kristen.gif"
);
?>
The embedded PHP into your XHTML:
<?php
echo '<img
src="'.randomImage($my_images).'"
alt="Random Image" />';
?>
Why did I bother writing this? Because I’ve seen way too many scripts and downloads and all that yada yada just to make some simple random image rotator.
If you want to add more images, just put in an extra item into the array.
Case closed.




December 13th, 2004 at 3:57 am
I always wondered how to do that……
lol.
October 23rd, 2005 at 3:07 pm
I need to know how to make like a mini slide show that automatically flips through images in a folder. I mean live, like in intervals.
December 23rd, 2005 at 5:33 am
The problem with this code is that the random function will often pick the same pic from the array 2 or 3 times in a row before choosing a different pick.
January 26th, 2006 at 3:08 pm
And how to to a non-random rotator, meaning: starting from the first in line and when reaching the alst in the array, restarting again. and maybe a setTimeOut
June 29th, 2006 at 7:06 pm
Great job. I bookmarked this a while ago and I figured I should comment.
February 25th, 2007 at 2:40 pm
How about doing it with a folder with tons of images??
July 16th, 2007 at 11:42 am
I have just started with doing PHP and your code looks interesting to try for a resolvig of a problem i’ve.