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
https://forums.phpfreaks.com/topic/266201-in_array-not-working/
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);

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?

		$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.