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

2番目の回答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 に答える