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

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.