Jump to content

in_array not working..


bugzy

Recommended Posts

	
<?php

$ic = "Select category_id from item_category where item_id = {$edit_id}";
	$ic_result = mysql_query($ic,$connection) or die (mysql_error());
	$ic_num = mysql_num_rows($ic_result);


	for($c = 0; $ic_num > $c; $c++)
	{		
	$ic_row[$c] = mysql_fetch_array($ic_result);
	}


for($i = 0; $cat_num > $i; $i++)
	{

			if(in_array(mysql_result($cat_result,$i,'cat_id'),$ic_row,TRUE))
			{

				echo "<input type=\"checkbox\" name=\"item_cat[]\" value=". mysql_result($cat_result,$i,'cat_id') . " checked=\"checked\" />"; 					echo mysql_result($cat_result,$i,'cat_name'). "<br>";

			}
			else
			{
				echo "<input type=\"checkbox\" name=\"item_cat[]\" value=". mysql_result($cat_result,$i,'cat_id') . " />"; 					
				echo  mysql_result($cat_result,$i,'cat_name'). "<br>";

			}

}


?>

 

 

It's bypassing the first condition and it goes straightly to the else condition...

 

Anyone?

Link to comment
Share on other sites

		$ic_row[$c] = mysql_fetch_array($ic_result);

//  ...

			if(in_array(mysql_result($cat_result,$i,'cat_id'),$ic_row,TRUE))

 

$ic_row is an array of arrays. You are searching for a scaler value; it will never be found.

 

You probably meant to do $ic_row[$c] = mysql_result($ic_result, $c);

Link to comment
Share on other sites

What exactly are you trying to achieve? Your code is unnecessarily complex.

 

Thorpe I have this merge table name "item_category" where all items' categories are stored. It's all stored using an html checkbox

 

What I'm trying to do, If I want to edit an item category,  I'm want to retrieve those categories and check all the those that are listed as category in a checkbox.

 

I'm trying to use "in_array" to store all the stored categories on an iteam.

 

so it's like

 

check all categories in_array(category1, category2) from table category using a forloop to display all categories.

 

 

I wonder if this is possible using in_array?

Link to comment
Share on other sites

		$ic_row[$c] = mysql_fetch_array($ic_result);

//  ...

			if(in_array(mysql_result($cat_result,$i,'cat_id'),$ic_row,TRUE))

 

$ic_row is an array of arrays. You are searching for a scaler value; it will never be found.

 

You probably meant to do $ic_row[$c] = mysql_result($ic_result, $c);

 

 

This solves it.

 

Thanks DavidAM!  :D

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.