0

我在下面有代码,用户在“课程”中键入,然后它将显示属于该特定课程的相关模块,然后它将显示属于所选模块的下拉菜单。

问题是它只在属于该模块的下拉菜单中显示一个会话,即使该特定模块下有许多会话。

例如下面是数据库“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

然后当用户从模块下拉菜单中选择模块“CHI2513”时,在会话下拉菜单中应该显示以下选项:

会话下拉菜单:

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

}


    ?>
4

1 回答 1

1

好吧,您正在ORDER BY对查询应用一个子句,它对我说 TPP 数据行是查询输出的最后一位数据。然后反过来告诉我查看构建 HTML 输出的循环。

在那个循环中,你在循环$output = "";的开头。每次循环都会重置它。你做同样的事情$moduleHTML

所以我可以看到$output在每个循环结束时回显如何正确显示结果。但是在你的调用下面的表格中modelHTML,对于结果的每个循环都在重置,因此否定了.=附加到它的概念。你应该做的是把

$moduleHTML在你的 foreach 之外,所以它最初在那里定义,然后保持其他一切相同。

例如:

$moduleHTML = "";
foreach(....){.....}

代替

foreach(....){....$moduleHTML = "";....}

它目前所做的只是保存最后一次运行,可以通过循环来输出你的小部件、模块、表单等。

于 2012-10-18T23:12:02.547 回答