I have an array which contains the categories for a particular article ($link_cat). I'm then using mysql_fetch_array to print out all of the categories available into a list with checkboxes. While it's doing this I want it to compare the value it's on, to a value from the other array. If there is a match, then it means that one of the categories applies to this article, and it should print out a line of code to apply the checked attribute. great! except it's not working =[
while ( $row = mysqli_fetch_array($results, MYSQLI_ASSOC) ){
$cat[$i] = $row['category'];
$cat_id[$i] = $row['cat_id'];
echo '<li><input type="checkbox" ';
$catCount = count($link_cat);
for ($ct = 0; $ct < $catCount; $ct++){
if ($cat_id[$i] == $link_cat[$ct]){
echo 'checked="checked" ';
}
}
echo 'name="' . $cat_id[$i] . '" />' . $cat[$i] . '</li>';
$i++;
}
I've never really done a nested loop before (I suspect thats the problem).
The problem seems to be that when this runs, $link_cat[0] which will have the first category to check against in it - doesn't register. It comes up blank. Printing out variables inside the for loop confirmed this. Any others [1] [2] etc, are fine. It's just [0]. But why? it doesn't seem to make any sense. I know there is something in there, because I printed the contents of array as I filled it it, just to check. Yet it doesn't just show during the loop. Any ideas?