bugzy Posted July 25, 2012 Share Posted July 25, 2012 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/266201-in_array-not-working/ Share on other sites More sharing options...
trq Posted July 25, 2012 Share Posted July 25, 2012 What exactly are you trying to achieve? Your code is unnecessarily complex. Quote Link to comment https://forums.phpfreaks.com/topic/266201-in_array-not-working/#findComment-1364158 Share on other sites More sharing options...
DavidAM Posted July 25, 2012 Share Posted July 25, 2012 $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); Quote Link to comment https://forums.phpfreaks.com/topic/266201-in_array-not-working/#findComment-1364159 Share on other sites More sharing options...
bugzy Posted July 25, 2012 Author Share Posted July 25, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266201-in_array-not-working/#findComment-1364160 Share on other sites More sharing options...
bugzy Posted July 25, 2012 Author Share Posted July 25, 2012 $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! Quote Link to comment https://forums.phpfreaks.com/topic/266201-in_array-not-working/#findComment-1364161 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.