I'm trying to generate a list of checkboxes based on two mySQL queries.
I have three tables:
Contacts(ContactID,Name,Email)
GroupNames(GroupNameID,GroupName)
Groups(GroupID[auto_increment,ContactID,GroupID)
What I want to do, is check which groups a person is in, and have the option of changing that.
Query1:
$result = mysql_query("SELECT * FROM GroupNames, $db);
$myrow = mysql_fetch_array($result);
Query2 (the WHERE condition is a variable from a form, but it's set to 1 in this example):
$result2 = mysql_query("SELECT GroupID FROM Groups WHERE ContactID=1, $db);
$myrow2 = mysql_fetch_array($result2);
Now I want to create all the checkboxes from Query1, and check the checkboxes the person is in (Query2)
My current code:
do {
do {
$k=$myrow[0];
$v=$myrow[1];
if(in_array($k, $myrow)) {
echo "<input type=\"checkbox\" name=\"$k\" checked>$v<br>\n";
}
else {
echo "<input type=\"checkbox\" name=\"$k\">$v<br>\n";
}
}while ($myrow2=mysql_fetch_array($result2));
} while ($myrow=mysql_fetch_array($result));
Any suggestions?