Hello, I want to display randome image on my index page, is there any script can display randome image?? Thanks
I made a simple script that displays a random image. You need to create a folder called "images" and place all the images you want to randomize in it. <? $images_dir = "images/"; $handle = opendir($images_dir); $r = 0; while ($file = readdir($handle)){ if(!is_dir($file) && !is_link($file)){ $image[] = $file; $r++; } } $i = mt_rand(0,$r-1); echo "<img src=".$images_dir.$image[$i]." />"; ?> Code (markup):
Oops, I forgot to add the part that generates the random number . I edited my previous post, and the script should work now.
$i = rand (0, $r-1); PHP: if there are 5 pictures in the array, your range would be 0 to 4 Oh, and mt_rand is 4 times faster than rand according to the manual.
While we're on the subject, you should also initialize $image before the while loop, otherwise a user could manipulate it via the URL. $image = array (); PHP:
or. tablo.image ++++++++++++++++++ ++id+++image_name+++ ++1+++12345.jpg+++++ ++2+++453.jpg+++++++ ++4+++def.jpg+++++++ $query=mysql_query("select id,image_name from image order by rand() limit 0,1"); $img=mysql_result($query,0,'image_name'); echo "<img src='img/$img' border='0' />"; PHP: