0

我在下面有这个查询:

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
4

2 回答 2

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 标签

于 2012-11-05T22:06:59.727 回答
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;
于 2012-11-05T22:14:08.757 回答