Jump to content

Only issue LI once


unemployment

Recommended Posts

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
				}
			}

		?>

 

 

Link to comment
Share on other sites

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
				}
			}

Link to comment
Share on other sites

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
}
}

?>

Link to comment
Share on other sites

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
}
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
			}

		?>

Link to comment
Share on other sites

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.




			
		
Link to comment
Share on other sites

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!

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.