Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/239281-show-image-if-sql-is-empty/
Share on other sites

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/

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" />';

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.