Question

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>";
}

enter image description here

Was it helpful?

Solution 3

Thanks to the folks who provided helpful pointers. After a short break from the project, I was able to look at it with fresh eyes, and I think I have the answer. It's working for me, anyway.

//      grab the names of the special requirements
        $specialRequirementNamesQuery = "SELECT DISTINCT SpecialRequirementName 
            FROM SpecialRequirements 
            ORDER BY SpecialRequirementName;" ;
        $specialRequirementNames = mysql_query($specialRequirementNamesQuery)
            or die(mysql_error());

//      create the checkboxes
        while ($srn = mysql_fetch_array($specialRequirementNames)) {            
            echo "<input type='checkbox' name='SpecialRequirementName[]' value='" 
                . $srn['SpecialRequirementName'] . "' ";

//          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());

//          compare the special requirement name to the selected special requirements
//          if they're the same, check the box
            while ($checkedBoxes = mysql_fetch_array($selectedSpecialRequirements)) {
                if(strcmp($srn['SpecialRequirementName'], $checkedBoxes['SpecialRequirementName'])==0) {
                    echo "checked";
                }
            }
            echo " /> " . $srn['SpecialRequirementName'] . "<br>";
        }

OTHER TIPS

the errors means that $checkedBoxes is not an array, please check if it is, or do a var_dump($checkedBoxes); before the foreach to check if it's set or not as an array. also, as randak said better switch to PDO, because mysql_ function are getting deprecated.

You could really simplify this code a lot by using a JOIN; also, as previous commenters have noted, you would be well served to move to either mysqli or PDO.

Since I prefer the latter, I've given a rough outline here of how your code might look using these tools together. Note that this is ROUGH - I haven't spent much time thinking about your specific problem, I'm just trying to illustrate an approach.

I have made some assumptions about your database schema - you can look at this gist to see the database schema my query was constructed against.

try {
    $db = new PDO('mysql:...'); // See php.net/pdo-mysql.connection for specifics
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $query = "select SpecialRequirementName as name, case when ra.StudentID = s.StudentID then 'checked=\"checked\"' else '' end as has_assigned
              from SpecialRequirements r 
              left outer join SpecialRequirementAssignments ra on (r.SpecialRequirementID = ra.SpecialRequirementID)
              left outer join Students s on (ra.StudentID = s.StudentID and s.StudentID = :StudentID)";
    $stmt = $db->prepare($query);
    $stmt->bindValue(':StudentID', $StudentID);
    $stmt->execute();
    while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
        echo "<input type='checkbox' name='SpecialRequirementName[]' value='".
        htmlentities($row['name']) . ' ' .
        ($row['has_assigned']) ? :
        '">';
    }
} catch (PDOException $e) {
    // There was a failure somewhere in the above,
    // you can examine $e for details, but should get in the
    // habit of logging these.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top