unemployment Posted April 8, 2011 Share Posted April 8, 2011 How can I make it say... if none of the values in the array = $is_assoicate then do stuff... Right now it adds an li every time a value is not in the array. I only want it to add in 1 LI <?php foreach($users_associates as $is_associate) { if ($is_associate['user_id'] != $user_info['uid']) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/ Share on other sites More sharing options...
dawsba Posted April 8, 2011 Share Posted April 8, 2011 foreach($users_associates as $is_associate) { if (($is_associate['user_id'] != $user_info['uid'])&&$count<=0) { $count++; ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } } Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198561 Share on other sites More sharing options...
unemployment Posted April 8, 2011 Author Share Posted April 8, 2011 that didn't work for me. $count is undefined. Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198563 Share on other sites More sharing options...
dawsba Posted April 8, 2011 Share Posted April 8, 2011 at the top of the script just add $count=0; Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198567 Share on other sites More sharing options...
unemployment Posted April 8, 2011 Author Share Posted April 8, 2011 still fails... It does seem to check all of the keys of the $is_associate array. Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198569 Share on other sites More sharing options...
unemployment Posted April 8, 2011 Author Share Posted April 8, 2011 This list the li several times, because it runs through all of the array values. Instead of issuing an li every time that ($is_associate['user_id'] != $user_info['uid']) I only want it to issue one li if none of the keys in the $is_associate['user_id'] array = $user_info['uid'] <?php foreach($users_associates as $is_associate) { if ($is_associate['user_id'] != $user_info['uid']) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198701 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 Try using the array_key_exists function. foreach($users_associates as $is_associate) { if (!array_key_exists($user_info['uid'], $is_associate['user_id'])) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } } Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198708 Share on other sites More sharing options...
unemployment Posted April 8, 2011 Author Share Posted April 8, 2011 That didn't seem to work. I got this error. Warning: array_key_exists() expects parameter 2 to be array, $user_info['uid'] is not an array. The only array here is the $users_associates array Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198709 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 That would have been nice to know.. you said it was. if (!array_key_exists($user_info['uid'], $users_associates)) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } Remember, this is checking if there's an array key that matches ($array['key'] = 'value'), not an array value. If you want to check for a value use in_array Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198718 Share on other sites More sharing options...
unemployment Posted April 8, 2011 Author Share Posted April 8, 2011 That would have been nice to know.. you said it was. if (!array_key_exists($user_info['uid'], $users_associates)) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } Remember, this is checking if there's an array key that matches ($array['key'] = 'value'), not an array value. If you want to check for a value use in_array Thanks... I did need in_array, but I still can't get it to work.. My output for $user_info['id']; = 1 echo '<pre>'; print_r($users_associates); Array ( [0] => Array ( [user_id] => 3 ) [1] => Array ( [user_id] => 14 ) [2] => Array ( [user_id] => 1 ) [3] => Array ( [user_id] => 4 ) [4] => Array ( [user_id] => 6 ) [5] => Array ( [user_id] => 19 ) ) The value is in the array, but yet, the li is still being added. <?php echo $user_info['uid']; echo '<pre>'; print_r($users_associates); if (!in_array($user_info['uid'], $users_associates)) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198727 Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 Right, because you have a multidimensional array there. in_array will only check one dimension. You were on the right track when you posted this, but since you just need to know if it's found try this: $found = false; foreach($users_associates as $associate) { if($associate['user_id'] == $user_info['uid']) { $found = true; break; } } if(!$found) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } I suppose you could also do this if each $users_associates contains just an array with a 'user_id' key. Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198733 Share on other sites More sharing options...
unemployment Posted April 8, 2011 Author Share Posted April 8, 2011 Right, because you have a multidimensional array there. in_array will only check one dimension. You were on the right track when you posted this, but since you just need to know if it's found try this: $found = false; foreach($users_associates as $associate) { if($associate['user_id'] == $user_info['uid']) { $found = true; break; } } if(!$found) { ?> <li class="bass"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </li> <?php } I suppose you could also do this if each $users_associates contains just an array with a 'user_id' key. That worked. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/233045-only-issue-li-once/#findComment-1198736 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.