I am creating a quiz system whereby a user enters a quiz title, and then questions and answers so that other users can complete them later on. I have 3 tables, one holding quiz names, 1 with the questions and 1 with answers. When I add the first quiz, everything displays fine e.g
question 1
answer 1
answer 2
answer 3 etc
The problem is when I add another quiz, the answers start duplicating e.g
question 1
answer 1
answer 1
answer 2
answer 2 etc
Could anyone help solve why this is happening. I assume its something to do with the for loop, but i dont know what
<?PHP
for($i = 0; $i < $numofrows; $i++) {
$row = pg_fetch_array($result2); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#666666\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#777777\">\n";
}
echo "
<p>".$row['questionno']."</p>
<p>".$row['question']."</p>
\n";
$query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref, quiz.quizref, quiz.name, quiz.createdby FROM questions
JOIN answers ON questions.questionno = answers.questionno JOIN quiz ON quiz.quizref = answers.quizref
WHERE questions.questionno = ".$row['questionno']. " AND quiz.quizref = ".$id;
$result3 = pg_query($query2) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows2 = pg_num_rows($result3);
for($j = 0; $j < $numofrows2; $j++) {
$row2 = pg_fetch_array($result3); //get a row from our result set
if($j % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#666666\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#777777\">\n";
}
echo "
<p><input type=\"radio\" name=\"" . $row['questionno'] . "\" id=\"selection[]\" value=\"" . $row2['answervalue'] . "\" />".$row2['answerno']. "–".$row2['answer']."</p>
\n";
}
}
?>
Thanks