2012-11-03 62 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 
相关问题