roldahayes Posted February 13, 2012 Share Posted February 13, 2012 (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/>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/257027-load-blank-image-if-nothing-found/ Share on other sites More sharing options...
litebearer Posted February 13, 2012 Share Posted February 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257027-load-blank-image-if-nothing-found/#findComment-1317586 Share on other sites More sharing options...
roldahayes Posted February 13, 2012 Author Share Posted February 13, 2012 I only need it to show the one image, That is still not working though, it is not loading the transparant.gif when it has nothing else to show... Quote Link to comment https://forums.phpfreaks.com/topic/257027-load-blank-image-if-nothing-found/#findComment-1317590 Share on other sites More sharing options...
litebearer Posted February 13, 2012 Share Posted February 13, 2012 Oops.. change this echo "<img src=\"images/transparent.gif/>"; to this echo "<img src='images/transparent.gif>'"; Quote Link to comment https://forums.phpfreaks.com/topic/257027-load-blank-image-if-nothing-found/#findComment-1317592 Share on other sites More sharing options...
roldahayes Posted February 13, 2012 Author Share Posted February 13, 2012 Excellent - Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/257027-load-blank-image-if-nothing-found/#findComment-1317598 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.