2012-11-05 75 views
0

我具有低於此查詢:如何在HTML表格顯示在不同行中的答案

SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, q.QuestionMarks 
FROM Session s 
INNER JOIN Question q ON s.SessionId = q.SessionId 
JOIN Answer an ON q.QuestionId = an.QuestionId 
AND an.SessionId = q.SessionId 
WHERE s.SessionName = "GHWSW1" AND q.QuestionId = 1 
GROUP BY an.SessionId, an.QuestionId 
ORDER BY q.QuestionId, an.Answer 

以上查詢輸出低於這個結果:

SessionId SessionName QuestionId QuestionContent   Answer QuestionMarks 
1   GHWSW1  1   Here are 2 answers  BD  5 

所以我包含在代碼查詢下方設置了一個HTML表:

$assessment = "GHWSW1"; 
$number = 1; 

$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, 
GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, q.QuestionMarks 
    FROM Session s 
    INNER JOIN Question q ON s.SessionId = q.SessionId 
    JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId 
    WHERE s.SessionName = ? AND q.QuestionId = ? 
    GROUP BY an.SessionId, an.QuestionId 
    ORDER BY q.QuestionId, an.Answer 
    "; 

    // prepare query 
    $stmt=$mysqli->prepare($query); 
    // You only need to call bind_param once 
    $stmt->bind_param("si", $assessment, $number); 
    // execute query 
    $stmt->execute(); 


     // This will hold the search results 
    $searchQuestionId = array(); 
    $searchQuestionContent = array(); 
    $searchAnswer = array(); 
    $searchMarks = array(); 

    // Fetch the results into an array 

    // get result and assign variables (prefix with db) 
    $stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionContent, $dbAnswer, $dbQuestionMarks); 
     while ($stmt->fetch()) { 
     $searchQuestionId[] = $dbQuestionId; 
     $searchQuestionContent[] = $dbQuestionContent; 
     $searchAnswer[] = $dbAnswer; 
     $searchMarks[] = $dbQuestionMarks; 
     } 

?>  

</head> 

<body> 



<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 
<?php 

echo "<table border='1' id='markstbl'> 
     <tr> 
     <th class='questionth'>Question No.</th> 
     <th class='questionth'>Question</th> 
     <th class='answerth'>Answer</th> 
     <th class='noofmarksth'>Total Marks</th> 
     </tr>\n"; 

      foreach ($searchQuestionContent as $key=>$question) { 

     echo '<tr class="questiontd">'.PHP_EOL; 
     echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL; 
     echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL; 
     echo '<td class="answertd">'.htmlspecialchars($searchAnswer[$key]).'</td>' ; 
     echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL; 
} 
     echo "</table>" . PHP_EOL; 

     ?> 

所以HTML表顯示它是這樣的:

QuestionId QuestionContent   Answer QuestionMarks 
1   Here are 2 answers  BD  5 

但我想它像這樣在HTML表:

QuestionId QuestionContent   Answer QuestionMarks 
1   Here are 2 answers  B  5 
            D 

第二個答案D應該是在一個單獨的行和其他行應該有一個行跨度。但是,這怎麼能實現呢?

會話表:

SessionId (auto) SessionName 
1     AAA 

問表:

SessionId QuestionId (auto) QuestionContent 
1   1     What is 2+2? 

回答表:

AnswerId (auto) SessionId QuestionId Answer 
1    1   1   B 
2    1   1   D 
+0

你不能,因爲你在查詢做GROUP_CONCAT,你正在破壞你所需要的信息來表明需要一個新的行。你可以使用'
'作爲分隔符。這會給你多行,但仍然只是一個單元格,節省你不得不計算rowspans。 –

+0

這就是我所說的:一個很好的格式化!而且,你的MySQL查詢真棒! (對不起,我只在MySQL中有一些小知識) –

+0

@MarcB沒關係,我弄明白了。需要擺脫GROUP CLAUSE,然後在foreach循環中包含一些代碼,以便它將顯示行中需要空白的空白信息。 – user1794342

回答

1

試試這個

 foreach ($searchQuestionContent as $key=>$question) { 

    echo '<tr class="questiontd">'.PHP_EOL; 
    echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL; 
    echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL; 
    echo '<td class="answertd">'.htmlspecialchars($searchAnswer[$key]).'</td>' ; 
    echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL; 
    echo '</tr>'; 
} 

您忘記關閉tr標籤

+0

我實際上只是想出了它。我必須擺脫一個group by子句並在foreach循環中添加一些東西。我將回答要顯示的問題。但是如果你能告訴我如何跨行,如果它們是空的,我會給你最好的答案。我會提供我的答案,以便您可以看到代碼。這裏是一個鏈接,顯示錶格的樣子[鏈接](http://helios.hud.ac.uk/u0867587/Mobile_app/individualmarks.php) – user1794342

+0

我發佈了我的答案,看看答案和鏈接我在評論 – user1794342

+0

中提供了你的鏈接被破壞,你的意思是,你想要顯示一行,即使它是空的? –

0

刪除GROUP BY子句,並添加使foreach循環是這樣的:

echo "<table border='1' id='markstbl'> 
     <tr> 
     <th class='questionth'>Question No.</th> 
     <th class='questionth'>Question</th> 
     <th class='answerth'>Answer</th> 
     <th class='answermarksth'>Marks per Answer</th> 
     <th class='noofmarksth'>Total Marks</th> 
     </tr>\n"; 
       $previous_question_id = null; 
      foreach ($searchQuestionContent as $key=>$question) { 
      if ($previous_question_id == $searchQuestionId[$key]) { 
      $searchQuestionId[$key] = ''; 
      $question = ''; 
      $searchMarks[$key] = ''; 
     }else{ 
      $previous_question_id = $searchQuestionId[$key]; 
     } 
     echo '<tr class="questiontd">'.PHP_EOL; 
     echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL; 
     echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL; 
     echo '<td class="answertd">'; 
     echo $searchAnswer[$key]; 
     echo '</td>' ; 
     echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL; 
     echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL; 
}  
     echo '</tr>'; 
     echo "</table>" . PHP_EOL; 
相關問題