johanlundin88 Posted November 26, 2007 Share Posted November 26, 2007 I want this script to get the pictures with the id of persons in a database, this works. But when there are no picture with correct id in the folder I want the script to show a "No picture"-picture, but this doesn´t work with this script. Why not? <?php $hamta = "SELECT * FROM persons ORDER BY name"; $resultat = mysql_query($hamta) or die("Error!"); while($rad = mysql_fetch_array($resultat)) { $pictures = glob('pictures/' . $rad['id'] . '.jpg'); if (count($pictures) > 0) { foreach($pictures as $picture) { echo '<img src="', $picture, '" alt="" />'; } } else { echo '<img src="pictures/no_picture_.jpg" alt="No picture" />'; } } mysql_free_result($resultat); ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 26, 2007 Share Posted November 26, 2007 whats the error message and this line echo '<img src="', $picture, '" alt="" />'; should be echo '<img src="'.$picture.'" alt="" />'; Quote Link to comment Share on other sites More sharing options...
marcus Posted November 26, 2007 Share Posted November 26, 2007 Could just use if(file_exists("filename")){ Quote Link to comment Share on other sites More sharing options...
johanlundin88 Posted November 26, 2007 Author Share Posted November 26, 2007 There is no error message, the "no picture"-thing just doesn´t show up. The "no picture"-thing works (but on all pictures) if I shange: if (count($pictures) > 0) { to: if (count($pictures) > 1) { How do I use if(file_exists("filename")){ Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 27, 2007 Share Posted November 27, 2007 note glob returns the file name and path is not included change this line echo '<img src="', $picture, '" alt="" />'; to echo '<img src="pictures/'.$picture.'" alt="" />'; try... Quote Link to comment Share on other sites More sharing options...
johanlundin88 Posted November 27, 2007 Author Share Posted November 27, 2007 That doesn't make any difference, it's still the same. The no-picture thing and the picture doesn't seam to work at the same time but both of them works separate.. Quote Link to comment Share on other sites More sharing options...
johanlundin88 Posted November 27, 2007 Author Share Posted November 27, 2007 I got this to work now, thanks for the help: $filename = '../pictures/' . $rad['id'] . '.jpg'; if (file_exists($filename)) { echo '<img src="'.$filename.'" alt="' . $rad['namn'] . ' Foto: ' . $rad['fornamn'] . ' ' . $rad['efternamn'] . ', " width=80 height=100 border=0 /><br>' . $rad['namn'] . ' '; } else { echo ' No picture '; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.