Forum Moderators: coopster
<?
// #### RANDOM PIC ######################################### $thumbstring = '';
$file_dir="pics/a10"; // DIRECTORY WITH THE PICS
$f_type="tm.jpg"; // FILE EXTENSION YOU WISH TO DISPLAY
$dir=opendir($file_dir);
while ($file=readdir($dir))
{
if ($file!= "." && $file!= "..")
{
$extension=substr($file,-6); // THIS DIGIT MUST MATCH THE NUMBER OF CHARACTERS SPECIFIED IN THE FILE EXTENSION ABOVE
if($extension == $f_type)
{
$thumbstring .= "$file¦";
}
}
}
srand((double)microtime()*1000000);
$arry_txt = explode("¦" , $thumbstring);
echo "<img src=\"".$file_dir."/".$arry_txt[rand(0, sizeof($arry_txt) -1)]."\">";
// #### END RANDOM PIC #########################################
?>
What happens when you try the code exactly? Do you get errors? Red crosses?
To grab a random piccie from a directory, try this:
$file_dir = "images/";
$read = opendir($file_dir);
while ($file= readdir ($read))
{
if ($file!= "." && $file!= "..")
{
$images[] = $file;
}
}closedir($file_dir);
shuffle($images);
echo "<img src=\" . $images[0] . "\" border=\"0\">\n";
Something simple like that should work. Grab the contents of the dir, put them in an array, shuffle them, then get the image in the first slot.
Hope that helps.
dc:)
"; // #### END RANDOM PIC #########################################?>
In PHP, as with other programming languages, you can comment out certain things. In PHP for example, the following are quoted out and would not parse:
//no display
#no display
With the following line:
"; // #### END RANDOM PIC #########################################?>
The closing tag has been quoted out, so I`m guessing thats the problem. This may have been better:
"; // #### END RANDOM PIC #################
?>
Let me know how you go with the code and if you have problems, post again. We are only too glad to help out. :)