I am trying to insert a list of records into a table based upon values being true or false in a 2D array. I can figure out how to loop through all the ones with a value of 1 and do an insert but I can't see how to a delete if there is a value of 0.
The app is basically a matrix of people and groups that they belong to. The table is a join table to link people_id and group_id. So I am trying to do:
person_id group_id
1 12
1 15
2 15
2 16
3 16
My HTML code auto generates the matrix using a loop for the following row for each person:
<input type=\"checkbox\" name=\"person_group_arr[$person_id][$group_id]\" value=\"1\">
My PHP code that receives this post is:
while(list($index_x, $dummy_x) = each($person_group_arr)) {
while(list($index_y, $dummy_y) = each($person_group_arr[$index_x])) {
$value = $person_group_arr[$index_x][$index_y];
$query = "
INSERT INTO group_person (
group_id
, person_id)
VALUES (
$index_y
, $index_x
)";
$result = mysql_query($query) or die ("Sorry, we are unable to assign people to groups at this point.<br><br>Please try again later.");
echo "The person is $index_x<br>\n";
echo "The group is $index_y<br>\n";
echo " person_group_arr[$index_x][$index_y] = $value<br>\n";
}
}
This works great. However, I also want to delete from the group_person table if a checkbox is unchecked. Am I missing something obvious? I can't see how to do a delete statement based on a value in the array.....
Thanks in advance, Mark.....