latempest Posted February 12, 2013 Share Posted February 12, 2013 HELP, I had some custom code written by a developer who is now unreachable. Part of the code creates a link to a document: foreach ($prop->epcs as $epc){ $html = $html . sprintf('<p><a href="%s">View EPC</a></p>',(string)$epc->epc); } The code is using an XML feed to poluate a property database. The issue I have is that this piece of code is generating a link even if there is no document present. I need it to only display the link if there is a value in the $epcs / $epc variable. Can anyone provide a piece of updated code that will only output the link if there is a value? Hope that makes sense! Thanks Lee Quote Link to comment https://forums.phpfreaks.com/topic/274387-only-display-when-there-is-something-there/ Share on other sites More sharing options...
dmcglone Posted February 12, 2013 Share Posted February 12, 2013 This should work for you (untested) Or something similar would. foreach ($prop->epcs as $epc){ if(empty($epc)){ echo ""; } else { $html = $html . sprintf('<p><a href="%s">View EPC</a></p>',(string)$epc->epc); } Quote Link to comment https://forums.phpfreaks.com/topic/274387-only-display-when-there-is-something-there/#findComment-1411928 Share on other sites More sharing options...
dmcglone Posted February 12, 2013 Share Posted February 12, 2013 forgot to close the loop! } Quote Link to comment https://forums.phpfreaks.com/topic/274387-only-display-when-there-is-something-there/#findComment-1411930 Share on other sites More sharing options...
cyberRobot Posted February 12, 2013 Share Posted February 12, 2013 For what it's worth, the first part of the if isn't necessary. <?php foreach($prop->epcs as $epc) { if(!empty($epc->epc)) { $html = $html . sprintf('<p><a href="%s">View EPC</a></p>',(string)$epc->epc); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274387-only-display-when-there-is-something-there/#findComment-1411952 Share on other sites More sharing options...
latempest Posted February 12, 2013 Author Share Posted February 12, 2013 AWESOME! Thank you so much the last entry worked perfectly! Thanks so much! Lee Quote Link to comment https://forums.phpfreaks.com/topic/274387-only-display-when-there-is-something-there/#findComment-1411964 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.