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

           ?>

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")){

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

';

}

 

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.