Jump to content

PHP Newbie needs some help with random images


squiggie

Recommended Posts

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'>";

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.