I'm working with three database tables:
- Students: Can be assigned zero to many special requirements
- SpecialRequirements: Names of all special requirements that can be assigned to a student
- SpecialRequirementAssignments: Includes an associated StudentID and the name of the special requirement
I'm making a page for editing student records. I want to show all the special requirements as checkboxes and check the checkboxes that have previously been assigned to the student. In other words, I want to check the boxes whose values are equal to the values in the SpecialRequirementAssignments
table.
I'm getting the following error: "Warning: Invalid argument supplied for foreach()"
. Have tried my best to use the correct foreach
syntax, etc., but it's still not working.
The relevant part of my code. Thanks in advance!
// grab the names of the special requirements
$specialRequirementNamesQuery = "SELECT DISTINCT SpecialRequirementName
FROM SpecialRequirements ORDER BY SpecialRequirementName;" ;
$specialRequirementNames = mysql_query($specialRequirementNamesQuery)
or die(mysql_error());
// grab the names of the special requirements that are selected for this student
$selectedSpecialRequirementsQuery = "SELECT SpecialRequirementName
FROM SpecialRequirementAssignments
WHERE StudentID = " . $StudentID . ";" ;
$selectedSpecialRequirements = mysql_query($selectedSpecialRequirementsQuery)
or die(mysql_error());
$checkedBoxes = mysql_fetch_array($selectedSpecialRequirements);
// create the checkboxes
while ($row = mysql_fetch_array($specialRequirementNames)) {
echo "<input type='checkbox' name='SpecialRequirementName[]' value='"
. $row['SpecialRequirementName'] . "' ";
// if the SpecialRequirementAssignment record is the same as the SpecialRequirementName record, check the box
foreach ($checkedBoxes as $value) {
if($value==$row['SpecialRequirementName']) {
echo "checked";
}
}
echo " /> " . $row['SpecialRequirementName'] . "<br>";
}