Page 1 of 1

small quiz problem - please help :o)

Posted: Fri Mar 04, 2005 9:09 pm
by warmasice
Hi there,

I have come across a quiz script.

When i click submit, it successfully outputs the total number of correct answers. But for each question the following message is always output:

"you didn't select an answer. The answer is ___ "

Here is the code:

Code: Select all

<?php

include("contentdb.php");

$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);

if (!$_POST&#1111;'submit']) &#123;


echo "<form method=post action=&#123;$_SERVER&#1111;'PHP_SELF']&#125;>";
echo "<table border=0>";


while ($row = mysql_fetch_array($display)) &#123;

$id = $row&#1111;"id"];
$question = $row&#1111;"question"];
$opt1 = $row&#1111;"opt1"];
$opt2 = $row&#1111;"opt2"];
$opt3 = $row&#1111;"opt3"];
$answer = $row&#1111;"answer"];

echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value="$opt1"></td><td>$opt2 <input type=radio name=q$id value="$opt2"></td><td>$opt3 <input

type=radio name=q$id value="$opt3"></td></tr>";

&#125;

echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";

&#125;

elseif ($_POST&#1111;'submit'])

&#123;

$score = 0;
$total = mysql_num_rows($display);
while ($result = mysql_fetch_array($display))


&#123;

$answer = $result&#1111;"answer"];
$q= 'q'.$result&#1111;'id'];
if($_POST&#1111;$q] == $answer)
&#123;
$score++;
&#125;

&#125;

echo "<p align=center><b>You scored $score out of $total</b></p>";
echo "<p>";

if ($score == $total) &#123;
echo "Congratulations! You got every question right!";
&#125;
elseif ($score/$total < 0.34) &#123;
echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
&#125;
elseif ($score/$total > 0.67) &#123;
echo "Well done! You certainly know your stuff.";
&#125;
else &#123;
echo "Not bad - but there were a few that caught you out!";
&#125;

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) &#123;

$question = $row&#1111;"question"];
$answer = $row&#1111;"answer"];
$q = $row&#1111;"q"];

echo "<tr><td><br>$question</td></tr>";

if ($$q == $answer)
&#123;
echo "<tr><td>&raquo;you answered $&#123;$q&#125;, which is correct</td></tr>";
&#125;
elseif ($$q == "") &#123;
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
&#125;
else &#123;
echo "<tr><td>&raquo;you answered $&#123;$q&#125;. The answer is $answer</td></tr>";
&#125;

&#125;
echo "</table></p>";



&#125;

?>
I think the if ($$q == $answer) statement is the problem, although i could be wrong. Surely the following variables dont make sense:

$$q and ${$q} and $q = $row["q"];

Kind of stuck guys. Your help would be appreciated.

Thank you.


feyd | bolding your entire post doesn't help us at ALL

Posted: Fri Mar 04, 2005 9:32 pm
by smpdawg
Was $$q supposed to be referring to something that came in from the POST? What does q represent in your table?

Posted: Sat Mar 05, 2005 10:57 am
by warmasice
yes $$q refers to a post variable.

This script is from here (with a tutorial explain wot 'q' is. http://widgetmonkey.com/detail.php?id=5. I dont have too much of an understanding of what he's tried to do with his code as im pretty new to php.

Please can you take a look at the link and see what 'q' refers to? i would really appreciate any guidance u provide.

thank you

Posted: Sat Mar 05, 2005 11:26 am
by thegreatone2176
I may be way off on these because the code is confusing me but you have

Code: Select all

$answer = $result&#1111;"answer"];
$q= 'q'.$result&#1111;'id'];
why are you adding an extra q to their answer?

like the other person said you probally are wanting to take one of the $ of $$q.

Also for variables read I am pretty sure you need sinqle quotes so for lines like

Code: Select all

"<tr><td><br>$question</td></tr>";
make it

Code: Select all

'<tr><td><br>' . $question . '</td></tr>';
Maybe that whole adding a q is throwing it off because unless you have a q before ever answer it will never be correct