2012-10-18 78 views
0

我具有低於其中,在「場」的用戶類型,那麼它將顯示屬於該特定過程相關的模塊的代碼,然後將顯示一個下拉菜單屬於選定的模塊。它僅在下拉顯示一個選項菜單

的問題是,它僅顯示在下拉菜單中屬於模塊一個會話,即使有特定的模塊下的會話。

例如低於是數據庫「會話」表:

會話表:

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

} 


    ?> 

回答

1

那麼你所申請的ORDER BY子句來查詢,這對我來說說數據的TPP排是查詢數據輸出的最後一位。然後,這反過來又讓我看看構建HTML輸出的循環。

在這種循環中,您在循環的開頭有$output = "";。每一次循環都會重新設置它。而你也是這樣做的$moduleHTML

所以我可以看到$output在每個循環結束時被回顯將會正確顯示結果。但是,在下面的調用modelHTML的形式通過,結果每次循環復位從而否定的.=概念追加到它。你應該做的是把你的foreach

$moduleHTML外面所以它的最初定義那裏,然後持續一切相同。

如:中

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

代替

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

什麼它做着目前僅保存最後的跑這麼通過循環說話輸出小部件,模塊,形式,等等。 。

+0

您好,我感動$ moduleHTML外循環就像你說的,但它仍然在下拉菜單中只顯示一個sessin,做我需要移動$ sessionHTML其循環之外呢? – user1723760

+0

如果我移動$ sessionHTML =「」;然後我得到許多下拉菜單,每個會話下拉菜單都有其自己的會話在其下拉 – user1723760

+0

是的,以及最初的'' – chris

相關問題