Jump to content

Array help


n000bie

Recommended Posts

Hi,

 

Code

<?php 
$a = '20,44,72,73';

$b = explode(',', $a);

$total = count($b);

$c = 20;

$d = in_array($c, $b);

if ($d) 
{
	if ($total==1) { echo 'There is only 1 record and will be redirected to (its) detail page.';}
	else { echo "There are more than 1 record";}
}

?>

 

What I am trying to do is,

if there is only 1 value in $a, do something

if there is more than 1 value in $a do something else

 

$a comes from a table field (this is the category id and is saved seperated with comma)

 

This code works if the value of $a = 20 (or any other single number)

 

It does not work if $a has more values seperated with comma.

 

Can anyone help me please.

Link to comment
Share on other sites

Are you saying the value of $a will be coming from a table?

 

You might want to consider using array('value','value','value')

 

And you can test the array with empty() ..

 

And if you are pulling all of the data from a table, you will probably want to use foreach()

Link to comment
Share on other sites

Yes I have done that

 

this is my actual code

 

if(isset($_REQUEST['brand']))
	{	
		$sSqlQuery = 'where published=?';

		$row = $tobj->find_all($sSqlQuery,array('Y'));

		if($tobj->_row_count>0)
		{

			foreach($row as $r)
			{
				$brand = explode(',',$r->brand_id);

				$myTotalBrand=count($brand);

				if(in_array($cat_id,$brand))		
				{
					//echo $myTotalBrand;  
					if ($myTotalBrand==1) 
					{
						echo "<div style='text-align:center; height:100px; margin-top:50px;'><img src='".SITE_URL."/images/loading10.gif' /><div>loading...</div></div>";
			echo "<script>document.location.href='".SITE_URL."/shop/".$r->slug."'</script>"; 
					}
					else 
					{
					  $count_check++;
					  if(($count_check%6)==0)
					  {
						  echo '<li style="margin-right:0px;">';
					  }
					  else
						  echo '<li>';
					  
					  echo "<a href='".SITE_URL."/shop/".$r->slug."'>";echo "<img src=\"$r->logo_img\">"; echo "</a>";
					  echo "<a href='".SITE_URL."/shop/".$r->slug."' class='fixht'>"; echo ucfirst($r->shop_title); echo "</a>";
					  echo '</li>';
					}
				} // end in array check
			}

		} // end row count

	}

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.