我在下面的代码中用户输入“课程”,然后它将显示属于该特定课程的相关模块,然后它将显示属于所选模块的下拉菜单。
问题是它只在属于该模块的下拉菜单中显示一个会话,即使该特定模块下有很多会话。
例如下面是数据库“Session”表:
会议桌:
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
然后当用户从Module下拉菜单中选择Module“CHI2513”时,然后在Session下拉菜单中显示以下选项:
会话下拉菜单:
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
但它只是在下拉菜单中显示一个会话,如下所示:
它正在显示的会话下拉菜单:
TPP 2012-09-11 09:00:00
那么如何在下拉菜单中显示与所选模块相关的所有会话?
以下是代码:
<?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>";
}
}
?>
答案 0 :(得分:1)
您正在向查询应用ORDER BY
子句,该子句告诉我TPP数据行是查询输出的最后一位数据。然后反过来告诉我看看构建HTML输出的循环。
在该循环中,您在循环开始时有$output = "";
。每次循环都会重置它。你用$moduleHTML
所以我可以看到在每个循环结束时回显$output
将如何正确显示结果。但是在你的调用modelHTML
下面的表单中,对于每个循环结果都会重置,从而否定了.=
附加到它的概念。你应该做的是
$moduleHTML
在你的foreach之外,所以最初在那里定义,然后保持其他一切。
例如:
$moduleHTML = "";
foreach(....){.....}
而不是
foreach(....){....$moduleHTML = "";....}
它当前所做的只是保存最后一次运行所以通过循环来说明你的小部件,模块,表格等等的输出。