I'm building an educational database to sign up users for various training classes.

each class has prerequisites.

So when a user selects a class from the calendar to register for, I'm running a check of their class history vs. the prereqs for the selected class.

I'm having trouble with the comparison though. The prereqs are stored as records in a table, with the parent class being the ID of chosen selection and the prereq class being the ID of the prereq.

The Class history for each user is stored in a string, which I break up into an array. I'm using array_diff to check to be sure all IDs from the array of prereqs is in the class history array.

if the result is null then it should let the user register. if its not null, I want to display the list of prereqs that aren't in the student's history.

Here's what I have

// check selection against User's class history and prerequisites
$sqlList = "SELECT PrereqSession FROM Prereqs WHERE ParentSession = '$ClassID' ";
$resultList = mysql_query($sqlList) or die ("Error in query sqlList: $sqlList. " . mysql_error());

// make an array out of the Prereq query results
$prereqs = array();
while($rowList = mysql_fetch_object($resultList)) {
   $prereqs[] = $rowList;
   }


// make array out of user's class history   
// check selection against User's class history and prerequisites $sqlH = "SELECT ClassHistory FROM Persons WHERE PersonID = '$PersonID' "; $resultH = mysql_query($sqlH) or die ("Error in query sqlH: $sqlH. " . mysql_error()); $rowH = mysql_fetch_object($resultH); $classes = explode(',', $rowH->ClassHistory); // get difference of arrays, retuns results from the first array that are not present in the second $results = array_diff($prereqs, $classes); if($results != '') { echo '<tr><td colspan=2><p><br><b>You are required to take the following Prerequisites before registering for the selected class. Please cancel to go back to the calendar or select another session from the drop down list:</b><p></td></tr>'; echo '<tr><td colspan=2><p>'; // print output with labels foreach($results as $result) { $sqlList = "SELECT Name FROM Classes WHERE ClassID = '$result' "; $resultList = mysql_query($sqlList) or die ("Error in query sqlList: $sqlList. " . mysql_error()); $rowList = mysql_fetch_object($resultList); echo 'Class ID: '.$result.' -- '.$rowList->Name.'<br>'; }
echo '</p></td></tr>'; }

    Only glancing at this, but...

    $results = array_diff($prereqs, $classes);
    
    
    if($results != '') { 

    First, testing to see if something is not an empty string is not the same as testing to see if it is not null. Second, array_diff() returns an array of all the elements in its first argument that do not appear in the second argument. And an array with no elements in it is not null (nor is it an empty string). Even more importantly, no array is ever equal to an empty string, so your test will always be true.

    If you want to see if $results is an empty array or not, then that is what you should test for:

    if($results != array())

    or

    if(!empty($results))

      OK, that does help with the business logic.

      Only if both arrays are not empty should array_diff be used.

        Thanks much.

        I think this has it. its not pretty/elegant, but it works.

        // check selection against User's class history and prerequisites
        $sqlList = "SELECT PrereqSession FROM Prereqs
        LEFT OUTER JOIN Classes ON Prereqs.PrereqSession = Classes.ClassID
        WHERE ParentSession = '$ClassID' ";
        $resultList = mysql_query($sqlList) or die ("Error in query sqlList: $sqlList. " . mysql_error());
        
        if(mysql_num_rows($resultList) > 0) {
        
        // make an array out of the Prereq query results
        $prereqs = array();
        while($rowList = mysql_fetch_object($resultList)) {
           $prereqs[] = $rowList->PrereqSession;
           }
        
        
        // make array out of user's class history   
        // check selection against User's class history and prerequisites $sqlH = "SELECT ClassHistory FROM Persons WHERE PersonID = '$PersonID' "; $resultH = mysql_query($sqlH) or die ("Error in query sqlH: $sqlH. " . mysql_error()); $rowH = mysql_fetch_object($resultH); $classes = explode(',', $rowH->ClassHistory); // get difference of arrays, returns results from the first array that are not present in the second $results = array_diff($prereqs, $classes); if($results !=array()) { echo '<tr><td colspan=2><p><br><b><font color="red">You are required to take the following Prerequisites before registering for the selected class. Please cancel to go back to the calendar or select another session from the above drop down list:</font></b><p></td></tr>'; echo '<tr><td colspan=2><p>'; // print output with labels foreach($results as $result) { $sqlList = "SELECT Name FROM Classes WHERE ClassID = '$result' "; $resultList = mysql_query($sqlList) or die ("Error in query sqlList: $sqlList. " . mysql_error()); $rowList = mysql_fetch_object($resultList); echo 'Class ID: '.$result.' -- '.$rowList->Name.'<br>'; }
        echo '</p></td></tr>'; // confirm VUnetID echo '<tr><td class="shade" width="125"><b>Your VUnetID:</b></td>'; echo '<td><input type="text" name="VUNETID" value="'.$_SESSION['UserID'].'" readonly></td></tr>'; echo '<tr> <td colspan=2 align="center"><br> <input type="reset" name="reset" value="Cancel" onclick=\'window.location.href="calendar.php"\'> </td> </tr>'; } else { echo '<tr><td colspan=2><p><br>If this is not correct, please select another session from the drop down box above, or click the cancel button below to return to the calendar.<p></td></tr>'; // confirm VUnetID echo '<tr><td class="shade" width="125"><b>Your VUnetID:</b></td>'; echo '<td><input type="text" name="VUNETID" value="'.$_SESSION['UserID'].'" readonly></td></tr>'; echo '<tr> <td colspan=2 align="center"><br> <input type="hidden" name="enter" value="1"> <input type="submit" name="Submit" value="Confirm"> <input type="reset" name="reset" value="Cancel" onclick=\'window.location.href="calendar.php"\'> </td> </tr>'; } } else { echo '<tr><td colspan=2><p><br>If this is not correct, please select another session from the drop down box above, or click the cancel button below to return to the calendar.<p></td></tr>'; // confirm VUnetID echo '<tr><td class="shade" width="125"><b>Your VUnetID:</b></td>'; echo '<td><input type="text" name="VUNETID" value="'.$_SESSION['UserID'].'" readonly></td></tr>'; echo '<tr> <td colspan=2 align="center"><br> <input type="hidden" name="enter" value="1"> <input type="submit" name="Submit" value="Confirm"> <input type="reset" name="reset" value="Cancel" onclick=\'window.location.href="calendar.php"\'> </td> </tr>'; }
          Write a Reply...