busnut Posted May 3, 2009 Share Posted May 3, 2009 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 More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 substr($im,-3)==$filetypes Can you do checking like that in PHP? I didn't know that. Hmm... Link to comment https://forums.phpfreaks.com/topic/156609-solved-if-no-photo-is-present-error/#findComment-824623 Share on other sites More sharing options...
busnut Posted May 3, 2009 Author Share Posted May 3, 2009 substr($im,-3)==$filetypes Can you do checking like that in PHP? I didn't know that. Hmm... Must be able to. The script works perfect, and does what it should, just can't work out that if no image present, echo this instead... Link to comment https://forums.phpfreaks.com/topic/156609-solved-if-no-photo-is-present-error/#findComment-824627 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2009 Share Posted May 3, 2009 The posted code creates an array of images in $image. You can use the empty function to test if the array is empty. Link to comment https://forums.phpfreaks.com/topic/156609-solved-if-no-photo-is-present-error/#findComment-824630 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Damn.. he beat me. Link to comment https://forums.phpfreaks.com/topic/156609-solved-if-no-photo-is-present-error/#findComment-824632 Share on other sites More sharing options...
busnut Posted May 3, 2009 Author Share Posted May 3, 2009 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! Link to comment https://forums.phpfreaks.com/topic/156609-solved-if-no-photo-is-present-error/#findComment-824642 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.