I have a loop that will get some questions and the answers for them.
I want the loop to only print the question once and then the 4 answers after before it prints the next question/answers.
So far it does that but is always a question out. Eg. It prints :
Q1
Q2
A to Q1
Q3
A to Q2
Blank
A to Q3
Here is the code :
[CODE] $result = mysqli_query($conn, “SELECT
q.QText, q.id AS QId, ua.id, a.AText, ca.id, ca.Answer_ID,
case when a.id = ua.Answer_ID then ‘x’ else NULL end as IsUserAnswer ,
case when a.id = ca.Answer_ID then ‘x’ else NULL end as IsCorrectAnswer
FROM user_answers ua
INNER JOIN question q ON q.id = ua.Question_ID
INNER JOIN answer a ON a.Question_ID = q.id
INNER JOIN correct_answer ca ON ca.Question_ID = q.id
WHERE ua.Test_ID=1
ORDER BY q.ID, ua.ID”) or die(mysqli_error($conn));
echo ‘<table class=”striped centered”>
<thead>
<tr>
<th>Answer</th>
<th>Your Answer</th>
<th>Correct Answer</th>
</tr>
</thead>’;
$lastQuestionID = 0;
while ($data2 = mysqli_fetch_array($result))
{
if ($data2[‘QId’] != $lastQuestionID) {
echo ‘<p>Q. ‘ . $data2[‘QText’] . ‘</p>
<table class=”layout display responsive-table”>
<thead>
<tr>
<th>Answer</th>
<th>Your Answer</th>
<th>Correct Answer</th>
</tr>
</thead>’;
$lastQuestionID = $data2[‘QId’];
}
else {
echo ‘
<tr>
<td>’ . $data2[‘AText’] . ‘</td>
<td>’ . $data2[‘IsUserAnswer’] . ‘</td>
<td>’ . $data2[‘IsCorrectAnswer’] . ‘</td>
‘;
}}
echo “</table><br />”;