2012-11-03 65 views
1

我對如何在我的PHP代碼顯示正確的表格佈局的問題:如何每一行中顯示的答案和文本輸入

我想在表格中顯示的答案和他們的文字輸入。現在在它顯示如下的時刻:

Question    Answer  Marks Per Answer  
What is 2+2   B    (text input)   
Name the 3 hobbits? BCE   (text input) 

我想更改表的顯示,使得它看起來像下面這樣:

Question    Answer  Marks Per Answer 
What is 2+2?   B    (text input) 
Name the 3 Hobbits? B    (text input)     
         C    (text input) 
         E    (text input) 
  1. 正如您可以從新顯示中看到的那樣。我希望每個問題的每個答案都顯示在他自己的行中,而不是每個問題中的每個問題的答案都在一行中,而這正是它現在正在做的事情。
  2. 我想要的文字輸入也可以在自己的行顯示,像答案:

我的問題是,如何可以點1和2來實現,以便它可以適應新的佈局?

下面是當前顯示的代碼:

$assessment = $_SESSION['id'] . $sessionConcat; 

include('connect.php'); 

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

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


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


     // Fetch the results into an array 

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

     ?>  

     </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</th> 
     <th class='answerth'>Answer</th> 
     <th class='answermarksth'>Marks per Answer</th> 
     </tr>\n"; 

     foreach ($searchQuestionContent as $key=>$question) { 
     echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL; 
     echo '<td class="answertd">'.htmlspecialchars($searchAnswer).'</td>' . PHP_EOL; 
     echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL; 
     } 
     echo "</table>" . PHP_EOL; 

     ?> 

     </form> 

     </body> 

下面是問表的樣子:

問表:

QuestionId (auto) QuestionContent 
1     What is 2+2? 
2     Name the 3 hobbits? 

回答表:

AnswerId (auto) QuestionId Answer 
1     1   B 
2     2   B 
3     2   C 
4     2   E 
+0

有關您的數據庫模式的更多信息將有所幫助。另外,你在哪裏使用綁定參數'「s」'?你是否包含了你的整個查詢? – slashingweapon

+0

讓我稍微編輯一下代碼和問題,這樣對你和其他人來說會更容易10分鐘 – CMB

+0

@slashingweapon和其他人,問題已更新爲包含更新的代碼,更新的示例和數據庫表的示例 – CMB

回答

0

它看起來像你可以擺脫分組子句。您可能需要通過問題ID進行排序,因此同一問題的所有答案都將一起給出。

SELECT q.QuestionId, q.QuestionContent, an.Answer 
FROM Question q 
INNER JOIN Answer an ON q.QuestionId = an.QuestionId 
WHERE s.SessionName = ? 
ORDER BY q.QuestionId, an.Answer 
相關問題