fife Posted March 17, 2011 Share Posted March 17, 2011 I want to echo this div below but only if there is more than one of these fields missing. How would I go about this? <?php //only echo all of this if one or more of these fields does not exists so....... if () { echo" <div id=\"items_todo\"> <h3>Members Info</h3><br/> <ul>"; if (empty($User['address_L1'])) { echo "<li><a href=\"#\">Update Address</a></li>";} elseif (empty($User['intro'])) {echo "<li><a href=\"#\">About You</a></li>";} elseif (empty($User['profile_image'])){echo "<li><a href=\"#\">Add Profile Image</a></li>";} elseif (empty($User['Nickname'])){echo "<li><a href=\"#\">Add A Nickname</a></li>";} echo"</ul> </div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230926-echo-if/ Share on other sites More sharing options...
WTFranklin Posted March 17, 2011 Share Posted March 17, 2011 I think you could just do: if (empty($User['address_L1']) || empty($User['intro']) || empty($User['profile_image']) || empty($User['Nickname'])) { echo" <div id=\"items_todo\"> <h3>Members Info</h3><br/> <ul>"; if (empty($User['address_L1'])) { echo "<li><a href=\"#\">Update Address</a></li>";} elseif (empty($User['intro'])) {echo "<li><a href=\"#\">About You</a></li>";} elseif (empty($User['profile_image'])){echo "<li><a href=\"#\">Add Profile Image</a></li>";} elseif (empty($User['Nickname'])){echo "<li><a href=\"#\">Add A Nickname</a></li>";} echo"</ul> </div>"; That way it will echo the div if one of those is empty and then move on the code you already have. -Frank Quote Link to comment https://forums.phpfreaks.com/topic/230926-echo-if/#findComment-1188711 Share on other sites More sharing options...
fife Posted March 17, 2011 Author Share Posted March 17, 2011 brill thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/230926-echo-if/#findComment-1188715 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2011 Share Posted March 17, 2011 Here's a simpler way (and cleaner way) of doing this: <?php $tst = array('address_L1'=>'Update Address','intro'=>'About You','profile_image'=>'Add Profile Image','Nickname'=>'Add A Nickname'); $tmp = array(); foreach ($tst as $item => $txt) { if (empty($User[$item])) { $tmp[] = "<li><a href='#'>$txt</a></li>"; } } if (!empty($tmp)) { // if $tmp is not empty a least one of the items tested was empty echo" <div id='items_todo'> <h3>Members Info</h3><br/> <ul>"; echo implode("\n",$tmp) . "\n"; echo"</ul> </div>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/230926-echo-if/#findComment-1188723 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.