I have code below where the user types in a “Course”, then it will display the relevant modules which belongs to that particular course and then it will display a drop down menu which belongs to the selected module.
The problem is that it only displays one session in the drop down menu which belongs to the module, even though there are a many sessions under that particular module.
For example is below is the database “Session” Table:
Session Table:
SessionId SessionDate SessionTime ModuleId
AAA 2012-10-19 09:00:00 CHI2513
AFD 2012-10-29 10:00:00 CHI2513
TPP 2012-09-11 09:00:00 CHI2513
NUM 2012-05-03 11:00:00 CHI2513
Then when the user selects Module “CHI2513” from the Module drop down menu, then in the Session drop down menu it should display the following option:
Session Drop Down menu:
AAA 2012-10-19 09:00:00
AFD 2012-10-29 10:00:00
TPP 2012-09-11 09:00:00
NUM 2012-05-03 11:00:00
But instead it is only showing one session in the drop down menu like below:
Session Drop Down menu it is showing at moment:
TPP 2012-09-11 09:00:00
So how can I display ALL of the sessions in the drop down menu which are relevant to the selected module?
Below is the code:
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = ?)
ORDER BY c.CourseName, m.ModuleId
";
$qrystmt=$mysqli->prepare($query);
// You only need to call bind_param once
$qrystmt->bind_param("s",$courseid);
// get result and assign variables (prefix with db)
$qrystmt->execute();
$qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);
$qrystmt->store_result();
$num = $qrystmt->num_rows();
if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
} else {
$dataArray = array();
while ( $qrystmt->fetch() ) {
// data array
$dataArray[$dbCourseId]['CourseName'] = $dbCourseName;
$dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName;
// session data
$_SESSION['idcourse'] = $dbCourseId;
$_SESSION['namecourse'] = $dbCourseName;
}
foreach ($dataArray as $courseId => $courseData) {
$output = "";
$output .= "<p><strong>Course:</strong> " . $courseId . " - " . $courseData['CourseName'] . "</p>";
$moduleHTML = "";
$moduleHTML .= '<select name="modulesDrop" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($courseData['Modules'] as $moduleId => $moduleData) {
$moduleHTML .= "<option value='$moduleId'>" . $moduleId . " - " .
$moduleData['ModuleName'] ."</option>".PHP_EOL;
}
}
$moduleHTML .= '</select>';
echo $output;
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Module: <?php echo $moduleHTML; ?><input id="moduleSubmit" type="submit" value="Submit" name="modulesubmit" /></p>
</form>
<?php
}
}
$sessionnum = 0;
if (isset($_POST['modulesubmit'])) {
if($_REQUEST['modulesDrop']==''){
echo "Please Select a Module from the Drop Down Menu Above";
}
else{
var_dump($_POST['modulesDrop']);
$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId
FROM Session
WHERE
(ModuleId = ?)
ORDER BY SessionDate, SessionTime
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("s",$_POST['modulesDrop']);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
$dataArraySession = array();
while ( $sessionqrystmt->fetch() ) {
$dataArraySession[$dbSessionId]['SessionDate'] = $dbSessionDate;
$dataArraySession[$dbSessionId]['SessionTime'] = $dbSessionTime;
}
foreach ($dataArraySession as $sessionId => $sessionData) {
$sessionHTML = "";
$sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$sessionHTML .= "<option value='$sessionId'>" . $sessionId . " - " . $sessionData['SessionDate']. " - " . $sessionData['SessionTime'] ."</option>".PHP_EOL;
}
$sessionHTML .= '</select>';
}
if ($sessionnum > 0) {
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Sessions: <?php echo $sessionHTML; ?><input id="sessionSubmit" type="submit" value="Submit" name="sesionsubmit" /></p>
</form>
<?php
}
else {
echo "<p>Sorry, You have No Sessions under this Module</p>";
}
}
?>
Well You are applying an
ORDER BYclause to your query, which says to me the TPP row of data is the last bit of data output by the query. Which then in turn tells me to look at your loop thats building the HTML output.In that loop you have
$output = "";in the begining of the loop. Which is reseting it every pass of the loop. And you do the same thing with$moduleHTMLSo I can see how
$outputbeing echoed at the end of every loop through will show the results correctly. But in the form below your callingmodelHTMLwhich for every loop through the results is resetting thus negating the concept of.=to append to it. What you should do is put$moduleHTMLoutside of your foreach so its initially defined there, then keep everything else the same.eg:
instead of
What its doing currently is only saving the last run so to speak through the loop for output in your widget, module, form, whatever..