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