Im using the following code. It goes through a DB and pulls out the question and the related answers.
However it displays the question each time it prints an answer.
How can I make it so the question is only printed once and then the answers are printed.
$qna = mysqli_query($conn, "SELECT q.QText, a.id, a.AText FROM question q INNER JOIN answer a ON q.id = a.Question_ID WHERE q.id=1") or die(mysqli_error($conn));
while ($data2 = mysqli_fetch_array($qna)) {
echo '<td>'.$data2['QText'].'</td>';
echo '
<table class="layout display responsive-table">
<thead>
<tr>
<th>ID</th>
<th>Answer</th>
</tr>
</thead>
<tr>
<td>'.$data2['id'].'</td>
<td>'.$data['AText'].'</td>
<td>'.$data['Group_ID'].'</td>
</table>
';
}
Select an ID from question table aswell like:
SELECT q.QText, q.id AS QId, a.id, a.AText FROM ...
And then you can print out the question only once using condition:
$lastQuestionID = 0;
while ($data2 = mysqli_fetch_array($qna)) {
if($data2['QId'] != $lastQuestionID)
echo '<td>'.$data2['QText'].'</td>';
$lastQuestionID = $data2['QId'];
// printing answer here
}