petenaylor Posted June 13, 2011 Share Posted June 13, 2011 Hi all I need to show a blank image if the SQL table is empty or a certain image if it has a entry in the SQL table. Here's my code: <?php if(empty($showpromos['productid'])) { ?> <img src="images/promotions/blank-promotion-image.jpg" width="310" height="150" /> <?php } ?> <?php if(!empty($showpromos['productid'])); { ?> <img src="images/promotions/<?php echo $showpromos['image']; ?>" width="310" height="150" /> <?php } ?> At the moment it doesn't show the blank promotion image? Just a missing image. Thanks for your help! Pete Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 So, you are saying that the correct image is shown when there are records? The fact that it shows a "missing image" means that the image tag is getting generated. Since you are hard coding the missing image URL that tells me that the URL is incorrect. Check the HTML source and check the path and name of the image. Are you running this from a page where there is no "images" folder in relation to that page? I always get this backwards, but if the url is NOT proceeded by a "/" then the path is relative from the current page. If you include a "/" at the beginning of the URL then the path is relative from the root of the site. So, if the images directory is located at www.mydomain.com/images and you are running this in the page www.mydomain.com/somefolder/index.php, then that code will make the page look for the image in www.mydomain.com/somefolder/images/ Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 13, 2011 Share Posted June 13, 2011 You have a semi-colon ; on the end of your if(); statement that ends that conditional statement at that point. If you were to write your php code without all those opening and closing php tags, it would be easier to see what your actual logic is - <?php if(empty($showpromos['productid'])){ echo '<img src="images/promotions/blank-promotion-image.jpg" width="310" height="150" />'; } else { echo '<img src="images/promotions/' . $showpromos['image'] . '" width="310" height="150" />'; } or more simply - <?php $file = (empty($showpromos['productid'])) ? 'blank-promotion-image.jpg' : $showpromos['image']; echo '<img src="images/promotions/' . $file . '" width="310" height="150" />'; or even - <?php echo '<img src="images/promotions/' . ((empty($showpromos['productid'])) ? 'blank-promotion-image.jpg' : $showpromos['image']) . '" width="310" height="150" />'; Quote Link to comment Share on other sites More sharing options...
petenaylor Posted June 13, 2011 Author Share Posted June 13, 2011 Wow! That's great, any thanks for your help! Quote Link to comment 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.