petenaylor Posted April 26, 2012 Share Posted April 26, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 Look at the source of the page or echo $node->field_img['und'][0]['filename'] to see if it contains what you are expecting. Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 26, 2012 Share Posted April 26, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 26, 2012 Share Posted April 26, 2012 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'] 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.