random

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
OX 3
Forum Newbie
Posts: 22
Joined: Fri Jan 02, 2004 7:58 pm
Location: El Paso, TX

random

Post by OX 3 »

i was wondwerin how to make a random image appear on a site
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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;
Post Reply