Jump to content

Show if not empty


petenaylor

Recommended Posts

Hi all

 

I have a pice of code that I want to only show the image if it's empty:

 

<?php if(!empty($node->field_img['und'][0]['filename'])); { ?>

<li><img src="http://mysite.co.uk/sites/default/files/brand-images/<?php print $node->field_img['und'][0]['filename']; ?>" style="max-width:270px; text-align:center;" /></li>

<?php } ?>

 

However, it shows an image placeholder (Red cross) even when it's empty?

 

Many thanks for your help

 

Pete

Link to comment
Share on other sites

Depending on which version of PHP you are using, empty() doesn't like being passed string offsets in arrays. You may want to consider storing your object property in a single variable and then checking to see if that variable is empty. Then you can also just use that variable for inserting it into your HTML when needed.

Link to comment
Share on other sites

Hi all

 

I have a pice of code that I want to only show the image if it's empty:

 

<?php if(!empty($node->field_img['und'][0]['filename'])); { ?>

<li><img src="http://mysite.co.uk/sites/default/files/brand-images/<?php print $node->field_img['und'][0]['filename']; ?>" style="max-width:270px; text-align:center;" /></li>

<?php } ?>

 

However, it shows an image placeholder (Red cross) even when it's empty?

 

What you are saying and what you have coded are two different things. You state you only want to show an image if it is empty. That makes no sense. If the image is empty how would you show it. Now, if you were going to show a default image if the image was empty that would make sense. but, that is not what you stated and that is not what you coded.

 

What you have coded would show the image if it is NOT empty - which makes more sense. If you are getting an image placeholder then the condition is apparently resulting in true.

 

So, there are two probable issues that I can think of; both of which you can verify by inspecting the source code as jesirose suggests.

 

The first one is that the value is not really an image name or not an image name that actually exists. So, check the source to see what the value is.

 

The second problem is that the name is correct for an actual image, but the path is incorrect. Double check the actual URL that you can type in your browser to access the images directly and make sure that is what you are using.

 

If the 'name' in the HTML source is empty you may want to verify what the actual value is. Try doing a var_dump() on $node->field_img['und'][0]['filename']

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.