Jump to content

[SOLVED] Show pictures with correct filename, else "No picture"


johanlundin88

Recommended Posts

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);

           ?>

Link to comment
Share on other sites

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

';

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.