Hi! I'm having some problems with checkboxes at the moment, and i'd be glad if you
could help me solve it... i'll give a brief explanation of the situation first so that
you can understand what i'm talking about :-)
I have the tables 'User' (containing userid,etc), 'Category' (containing
category_id) and the table User_Category (user)id and category_id) since i have a
many-to-many relationship between user and category
Now, when i add a new user, i display a list of the category names, with a checkbox
next to each, so that the user can check more than 1 category
Now, the problem: let's say i want to modify a particular user.... i thus have a
link on the user, e.g.
<a href="modify.php?id='.$row["user_id"].'">'.$row["name"].'</a>
Now, on modify.php, i want to display all the values that the user posted initially
(lastname, firstname, username, password, etc...)
Here i also want to display all the category names with a checkbox next to each (this
is easy!)
$sql = "SELECT * FROM category";
$result = mysql_query($sql);
// (this is referenced as LOOP1 below)
while ($row = mysql_fetch_array($result))
{
echo $row["name"];
echo '<input type="checkbox" name="cat[]" value='.$row["category_id"].'>';
}
Now, to get the values of category_id that the user previously selected, i just need
to do
$qry = "SELECT category_id FROM user_category WHERE user_id='$id'";
$result2 = mysql_query($qry);
// for example, i have 8 records in my table category, therefore i'll have 8
checkboxes with id values 1, 2...8
// now assume that, when filling his form, the user clicked the checkboxes with id
values 1, 2, 3, 4, 5 only
NOW ==>>> THE PROBLEM: (finally we reach there!!!) :-)
HOW do i modify LOOP1 so that i loop the second query $qry to validate which value of
category_id in each checkbox corresponds to the values returned by my query $qry, and
i check ONLY THOSE checkboxes, and leave the other checkboxes as unchecked?
I'd be grateful if you could help
Thanks and regards,
Yog.