Page 1 of 1

random

Posted: Thu Jan 08, 2004 10:45 pm
by OX 3
i was wondwerin how to make a random image appear on a site

Posted: Thu Jan 08, 2004 11:16 pm
by uberpolak
There are a couple of ways to do this, the easiest is probably to store the filenames of each of the possible images in an array, then use code like this (save as a script in the same folder as the images).

Code: Select all

<?php

//Assign each image to a value in the array
$pics = array('img1.jpg', 'img2.jpg', 'img3.jpg');

//Choose a random number to use with the array
srand((double)microtime()*1000000);
$img = rand(0, sizeof($pics) - 1);

//Send the page the right file
header('Location: ' . $pics[$img]);

?>
Then, in your HTML, do this:

Code: Select all

&lt;img src="scriptname.php' /&gt;