0

Hi I am relatively new to php and sql, I am creating a store locator and part finder, so the user needs to find somewhere that sells screw it will show them the details of that screw and the nearest store within 100 miles, it is all going well. What I am trying to do now is give each part an image so you can see what it looks like. ATM I have HTML embedded within the php this is my code.

                    echo "<div style=\"font-family:verdana;font-weight:bold;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
                            echo $row['partnum']."<br/>";
                    echo "</div>";  
                            echo"<img style=\"width:50%;\"; src=\"productimages\\".$row['partnum'].".png\" alt=\"Save icon\"/>";
                    echo "<div style=\"font-family:verdana;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\">";
                            echo $row['description'];
                    echo "</div>";

What I am trying to do is if my php looks for the image that I have got within my code and does not find it currently it shows a broken image link which is understandable but what I would like to do is if this happens I would like it to display an image just for the ones which do not have an image from my code.

sorry about poor english and not indenting my code

2 Answers 2

2

Just use file_exists:

if(file_exists($_SERVER['DOCUMENT_ROOT'].'\productimages\'.$row['partnum'].'.png')) {
  echo "<img style=\"width:50%;\"; src=\"productimages\\".$row['partnum'] .".png\" alt=\"Save icon\"/>";
} else {
  echo "No image found"; }
3
  • how would i get this to display another image instead of echoing no image found
    – Tom Parry
    Oct 9, 2013 at 16:01
  • Just put anything instead of echo I've presented. Oct 9, 2013 at 16:04
  • i fixed the problem it was my coding i have missed a ; on a line above just wanted to know instead of it just showing another image how do i get it to replace the image that is not there because at the moment it just prints the image in a different area and still hows the broken link from the original.
    – Tom Parry
    Oct 10, 2013 at 8:25
0

I second Moby04's answer and if you don't want to show the "No image found" text, you can simply show a "not-found.png" which also might work!

1
  • Actually No image is just example. :) However though if you want to use "not-found.png" it's much better idea to use 404 redirect for specific subdirectory in .htaccess file. Oct 9, 2013 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.