PDA

View Full Version : easy PHP



sanman918
April 30th, 2004, 02:04 PM
Hey,
It's been so long since I had to use any PHP. I just want to creat a webpage with images on it and everytime you refresh it brings up a random picture. I know how to do the effect but I dont remember the exact code. I know it has to do something with creating a varaible such a "num" and have it randomly create a number lets say 1-5. So in HTML i put image$num.jpg. But I forgot the code to do this. Can anyone help me out?
Thanks

hamza84
April 30th, 2004, 05:09 PM
if u have a collection of images say from 0-100, do this:

$num = rand(0, 100);

and then in HTML put images$num

Unit
May 1st, 2004, 04:17 PM
Or there's a more automated way:


<?
$dir = "images/"; // the directory where all the images are
if (is_dir($dir)) {
if($ar = opendir($dir)) {
while (false !== ($file = readdir($ar))) {
$if = explode(".", $file);
if ($if[1] == "gif" || $if[1] = "jpg" || $if[2] == "png") {
$img[] = $file;
}
}
}
}
$rand = mt_rand(0, count($img));
echo "<img src=\"" . $dir . $img[$rand] . "\" alt=\"My Image\" />";
?>

This will do all the work for you, so all you have to do is add images to the directory.

GreenLantern
May 1st, 2004, 04:37 PM
Wow, that is sweet. Nice code Unit.

SeiferTim
May 1st, 2004, 05:32 PM
Man... I need to start learning PHP... I just haven't had time to.... :!:

sanman918
May 1st, 2004, 08:00 PM
yeah thats some nice coding, I ended up using a bit of it, I got it to work. thanks for all the replies