/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Print Only One In While Loop

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 />”;
[/CODE]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @oxhey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...