text being shown in the wrong place

A

Anonymous

Guest
i have a quiz results page which checks each answer and echos the right answer if its wrong etc.

i have ten of these and then at the end i just want it to display the final score and a link but it always place it above the 10th echo answer and i have no idea on why its doing it and ive tried everything i can think of.

heres my code...

PHP:
<?php $queryq8 = "Select * from natquiz WHERE QNO ='$randomqno8'";

$resultq8 = mysql_query($queryq8);

while ($row = mysql_fetch_array($resultq8)) {
   $qno8 = $row['QNO'];
   $question8 = $row['Question'];
   $ansa8 = $row['AnsA'];
   $ansb8 = $row['AnsB'];
   $ansc8 = $row['AnsC'];
   $ansd8 = $row['AnsD'];
   $ans8 = $row['Answer'];
}



    if ($question8result == $ans8) { 
    	echo "<table border=0 width=100%>";
        echo "<tr><th><h3 align=left><img src=\"resources/quizcorrect.jpg\" />You answered Question: 8 {$question8result} - This is Correct! </th></tr>\r\n"; 
        $score++; 
    } else { 
    	echo "<table border=0 width=100% bgcolor=c0c0c0>";
        echo "<tr><th><h3 align=left><img src=\"resources/quizincorrect.jpg\" />You answered Question: 8 {$question8result} - This is Incorrect, The Correct answer is {$ans8}</th></tr>\r\n"; 
    
} 


?>

<br>

<?php $queryq9 = "Select * from natquiz WHERE QNO ='$randomqno9'";

$resultq9 = mysql_query($queryq9);

while ($row = mysql_fetch_array($resultq9)) {
   $qno9 = $row['QNO'];
   $question9 = $row['Question'];
   $ansa9 = $row['AnsA'];
   $ansb9 = $row['AnsB'];
   $ansc9 = $row['AnsC'];
   $ansd9 = $row['AnsD'];
   $ans9 = $row['Answer'];
}



    if ($question9result == $ans9) { 
    	echo "<table border=0 width=100%>";
        echo "<tr><th><h3 align=left><img src=\"resources/quizcorrect.jpg\" />You answered Question: 9 {$question9result} - This is Correct! </th></tr>\r\n"; 
        $score++; 
    } else { 
    	echo "<table border=0 width=100% bgcolor=c0c0c0>";
        echo "<tr><th><h3 align=left><img src=\"resources/quizincorrect.jpg\" />You answered Question: 9 {$question9result} - This is Incorrect, The Correct answer is {$ans9}</th></tr>\r\n"; 
    
} 

?>

<br>

<?php

$queryq10 = "Select * from natquiz WHERE QNO ='$randomqno10'";

$resultq10 = mysql_query($queryq10);

while ($row = mysql_fetch_array($resultq10)) {
   $qno10 = $row['QNO'];
   $question10 = $row['Question'];
   $ansa10 = $row['AnsA'];
   $ansb10 = $row['AnsB'];
   $ansc10 = $row['AnsC'];
   $ansd10 = $row['AnsD'];
   $ans10 = $row['Answer'];
}



    if ($question10result == $ans10) { 
    	echo "<table border=0 width=100%>";
        echo "<tr><th><h3 align=left><img src=\"resources/quizcorrect.jpg\" />You answered Question: 10 {$question10result} - This is Correct! </th></tr>\r\n"; 
        $score++; 
    } else { 
    	echo "<table border=0 width=100% bgcolor=c0c0c0>";
        echo "<tr><th><h3 align=left><img src=\"resources/quizincorrect.jpg\" />You answered Question: 10 {$question10result} - This is Incorrect, The Correct answer is {$ans10}</th></tr>\r\n"; 
    
} 

?>

<br>

<h3 align="center">You Got <?php echo $score ?> Questions Correct!</h3>
<h2 align="center">Save Quiz Score? <a href="resources/setquizcookie.php?score=<?php echo $score ?>">Click Here</a></h2>




</body>

the whole page follows the same pattern till the end but when viewed in a browser the bottom lines appear above the 10th statement

any ideas? cheers for any help
 
in this line echo "<table border=0 width=100%>"; set border = 1 and check what the table contains
 
i tried it and it contains the right echo saying the question is right
 
You don't close the tables that you start.

Code:
<?php
echo "<table border=0 width=100% bgcolor=c0c0c0>";
echo "<tr><th><h3 align=left><img src=\"resources/quizincorrect.jpg\" />You answered Question: 10 {$question10result} - This is Incorrect, The Correct answer is {$ans10}</th></tr>\r\n";
?>

Should be
Code:
<?php
echo "<table border=0 width=100% bgcolor=c0c0c0>";
echo "<tr><th><h3 align=left><img src=\"resources/quizincorrect.jpg\" />You answered Question: 10 {$question10result} - This is Incorrect, The Correct answer is {$ans10}</th></tr>\r\n";
echo "</table>";  // add this for every table that you started
?>

By the way... why aren't you using a loop here?

Coditor
 
cos im not very good lol

cheers for the help i'll give it a go now
 
There is probably a better way but I'd need to see how you fill variables like $randomqno8 and $question8result. For now, this should make things easier for you:

Code:
<?php

for ($n = 1; $n <= 10; $n ++) {
	$questionVariable = '$randomqno' . $n;
	$questionNumber = $$questionVariable; 
	$questionResultVariable = '$question' . $n . 'result';

	$getQuestionQuery = "SELECT Answer FROM natquiz WHERE QNO = " . $questionNumber;
	$getQuestionCursor = mysql_query($getQuestionQuery);

	if ($questionRow = mysql_fetch_array($getQuestionCursor)) {
		if ($$questionResultVariable == $questionRow['Answer']) {
			?>
			<h3><img src="resources/quizcorrect.jpg" border="0" alt="" />You answered Question: <?= $n ?> <?= $$questionResultVariable ?> - This is Correct!</h3>
			<?php
		} else {
			?>
			<div style="width:100%;background:#c0c0c0">
			<h3><img src="resources/quizincorrect.jpg" border="0" alt="" />You answered Question: <?= $n ?> <?= $$questionResultVariable ?> - This is Incorrect, The Correct answer is <?= $questionRow['Answer'] ?></h3>
			</div>
			<?php
		}
	}
}

?>

Coditor
 
Back
Top