squiggie Posted March 18, 2007 Share Posted March 18, 2007 I'm really brand new to PHP (like as of today) and I need some help. I need to have a simple random image script display images in the sidebar of a page I'm making and I've got it done but there is a bug. It mostly works but every once and a while instead of showing an image it shows .. Here is the code. It is basically repeated 3 times. Now in the page I call this function each time I want an image, and by calling different functions for different dirs, I know I won't get the same image. I know this is terrible code and there is a ton of ways to do this better, but I've already got it throughout the site and it would be a huge pain to change it now rather the figure out what the problem is. Can anyone offer advice? If you want an example of the page, go to http://www.aundriaspa.com/fitness.php. function get_random_image1($dir){ $dh = opendir($dir); /* The following loop scans the directory specified ignoring folders and Thumbs.db */ while (false !== ($filename = readdir($dh))) { if($filename == "Thumbs.db" || is_dir($filename)){ }else{ $files[] = $filename; } } closedir($dh); /* Generate a random number */ $nooffildi = count($files); $nooffiles = ($nooffildi-1); srand((double)microtime()*1000000); $randnum = rand(0,$nooffiles); /* print the result */ echo "<IMG SRC='$dir/$files[$randnum]' ALT='$files[$randnum]' BORDER='0'>"; } function get_random_image2($dir){ $dh = opendir($dir); /* The following loop scans the directory specified ignoring folders and Thumbs.db */ while (false !== ($filename = readdir($dh))) { if($filename == "Thumbs.db" || is_dir($filename)){ }else{ $files[] = $filename; } } closedir($dh); /* Generate a random number */ $nooffildi = count($files); $nooffiles = ($nooffildi-1); srand((double)microtime()*1000000); $randnum = rand(0,$nooffiles); /* print the result */ echo "<IMG SRC='$dir/$files[$randnum]' ALT='$files[$randnum]' BORDER='0'>"; } function get_random_image3($dir){ $dh = opendir($dir); /* The following loop scans the directory specified ignoring folders and Thumbs.db */ while (false !== ($filename = readdir($dh))) { if($filename == "Thumbs.db" || is_dir($filename)){ }else{ $files[] = $filename; } } closedir($dh); /* Generate a random number */ $nooffildi = count($files); $nooffiles = ($nooffildi-1); srand((double)microtime()*1000000); $randnum = rand(0,$nooffiles); /* print the result */ echo "<IMG SRC='$dir/$files[$randnum]' ALT='$files[$randnum]' BORDER='0'>"; } Link to comment https://forums.phpfreaks.com/topic/43284-php-newbie-needs-some-help-with-random-images/ Share on other sites More sharing options...
squiggie Posted March 19, 2007 Author Share Posted March 19, 2007 Anyone have any ideas? Link to comment https://forums.phpfreaks.com/topic/43284-php-newbie-needs-some-help-with-random-images/#findComment-210574 Share on other sites More sharing options...
fredb Posted March 19, 2007 Share Posted March 19, 2007 Just to point out another bug in your output, sometimes the same image does display. -Fred Link to comment https://forums.phpfreaks.com/topic/43284-php-newbie-needs-some-help-with-random-images/#findComment-210682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.