Jump to content

[SOLVED] if no photo is present error


busnut

Recommended Posts

G'day

 

I downloaded this snippet from the net that it searches a designated directory (which in my case depends on what the user has searched for), and if there is no photo, it just displays the img holder, but no image.

 

How can I get it that if there are no images in that directory, that it could display a default image like 'no image yet' or even just text of the same wording?

 

here is the code:

$dir = "photos/".$result['chassisbody']; // The directory where your images are
$filetypes = ("jpg"||"gif"); // Whatever images are valid. You could add “png” etc

srand((double)microtime()*123456789); // Generates a random number seed
$count = 0; // Initialises the counter

$dirOpen = opendir($dir); // Opens the image directory

while(($im = readdir($dirOpen)))
{
if($im != ".." && $im != "." && substr($im,-3)==$filetypes) // Disregards ".." and "." (direcrory structure)
{
$image[$count] = $im; // Reads an image
$count++; // Increments the counter
}
}

closedir($dirOpen); // Closes the directory

$randname = rand(0,(count($image)-1)); // Generates the random number
//echo "<img src='".$dir."/".$image[$randname]."' width='300'>"; // Publishes the image

Link to comment
https://forums.phpfreaks.com/topic/156609-solved-if-no-photo-is-present-error/
Share on other sites

ok, this is what i've done (took me a little while though to work out what var should be), and so far works perfectly:

$dir = "photos/".$result['chassisbody']; // The directory where your images are
$filetypes = ("jpg"||"gif"); // Whatever images are valid. You could add “png” etc

srand((double)microtime()*123456789); // Generates a random number seed
$count = 0; // Initialises the counter

$dirOpen = opendir($dir); // Opens the image directory

while(($im = readdir($dirOpen)))
{
if($im != ".." && $im != "." && substr($im,-3)==$filetypes) // Disregards ".." and "." (direcrory structure)
{
$image[$count] = $im; // Reads an image
$count++; // Increments the counter
}
}

closedir($dirOpen); // Closes the directory

$randname = rand(0,(count($image)-1)); // Generates the random number

if (empty($count)) {
    echo "Sorry, no image currently available for this bus";}

else echo "<a href='".$dir."/".$image[$randname]."' rel=\"lightbox\" title=\"".$result['chassisbody']."\" border=1><img src='".$dir."/".$image[$randname]."' border=0 width=300></a>";

 

Thanks for teh help guys! :)

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.