PDA

View Full Version : How to show default picture?



edhan
12-17-2008, 02:50 AM
Hi

I am wondering if anyone knows how I can display a default photo if the picture is not found in the database while extracting. The default photo is located in a different folder.
Example: domain.com/images/default.jpg

Here is my code:



<?php

$query1="SELECT `item`,`picture`,`code` FROM `Gallery` where `active` ='1' order by `item` DESC LIMIT 5";
$result1 = mysql_query($query1);

$num_rows = mysql_num_rows($result1);
for($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_array($result1);
$product=mysql_result($result1,$i,"item");
$photoid=mysql_result($result1,$i,"picture");
$ext=mysql_result($result1,$i,"code");

echo "Product: <img src='$siteurl/photo/$photoid.$ext'><a href='$siteurl/$product.html'><font color=Red>$product</font></a><br />
";
}
?>


Currently if the picture exists, it will display them but when the picture is non existence, it will not display (show as missing image). So, instead of not displaying, I am thinking of showing the default photo instead.

How can I do that? Any help is greatly appreciated.

DaveSawers
12-17-2008, 08:28 AM
<?php

$query1="SELECT `item`,`picture`,`code` FROM `Gallery` where `active` ='1' order by `item` DESC LIMIT 5";
$result1 = mysql_query($query1);

$num_rows = mysql_num_rows($result1);
for($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_array($result1);
$product=mysql_result($result1,$i,"item");
$photoid=mysql_result($result1,$i,"picture");
$ext=mysql_result($result1,$i,"code");

if (file_exists('$siteurl/photo/$photoid.$ext')) {
echo "Product: <img src='$siteurl/photo/$photoid.$ext'><a href='$siteurl/$product.html'><font color=Red>$product</font></a><br />";
} else {
echo "Product: <img src='$siteurl/nophoto/emptypic.jpg'><a href='$siteurl/$product.html'><font color=Red>$product</font></a><br />";
}
}
?>

edhan
12-18-2008, 02:40 AM
Hi DaveSawers

Tried your solution but it simply shows emptypic.jpg for all even though there are 2 other photos.

The 'code' is actually extension code for photos - i.e. jpg, gif or png. If no photo then the 'code' will be NULL.

Any method to use the 'code' for NULL to display the emptypic.jpg instead?

Thanks!

edhan
12-20-2008, 04:18 AM
if (file_exists('$siteurl/photo/$photoid.$ext')) {
echo "Product: <img src='$siteurl/photo/$photoid.$ext'><a href='$siteurl/$product.html'><font color=Red>$product</font></a><br />";

Seems like this code does not check for the photo existence as it simply shown the emptypic.jpg instead even though there is a photo.


} else {
echo "Product: <img src='$siteurl/nophoto/emptypic.jpg'><a href='$siteurl/$product.html'><font color=Red>$product</font></a><br />";
}


Anyway to ensure that file exist command really check for the existence before jumping into the else command to show the default photo?

Any suggestion is greatly appreciated.