Jump to content

Load blank image if nothing found...


roldahayes

Recommended Posts

(last question for this project...!)

 

This code loads and image from a URL stored in a database.

 

Some of the results pages wont have an image stored in them and I don't want it to load the "image not found" icon.  Therefore I have uploaded a transparent .gif file and would like that to load if no result was found in the database.

 

I am using IF and ELSE but this doesn't seem to work correctly - Can anyone advise on what I am doing wrong please.

 

Thanks

 

 

<?php

$van_query = "SELECT Image_Van  FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'";                                                             
					  
// the result of the query
$van_result = mysql_query($van_query) or die("Invalid query: " .mysql_error());


$pic = mysql_fetch_array($van_result);

// show the image


if ($van_query !== "")
echo "<img src='images/product_images/".$pic['Image_Van']."'/>";

else
echo "<img src=\"images/transparent.gif/>";

?>

Link to comment
https://forums.phpfreaks.com/topic/257027-load-blank-image-if-nothing-found/
Share on other sites

try...

<?php
$van_query = "SELECT Image_Van  FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'";                                                             
// the result of the query
$van_result = mysql_query($van_query) or die("Invalid query: " .mysql_error());
$pic = mysql_fetch_array($van_result);
// show the image
$my_image = trim($pic['Image_Van']);
if ($my_image != "") {
echo "<img src='images/product_images/" . $my_image ."'/>";
}else{
echo "<img src=\"images/transparent.gif/>";
}
?>

the above will show only 1 record, to show more you will need to incorporate a WHILE loop

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.