php code help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sangeetha
Forum Newbie
Posts: 8
Joined: Sat Mar 11, 2006 12:00 am

php code help

Post by sangeetha »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


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['submit']) {


echo "<form method=post action={$_SERVER['PHP_SELF']}>";
echo "<table border=0>";


while ($row = mysql_fetch_array($display)) {

$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$answer = $row["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>";

}

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

}

elseif ($_POST['submit'])

{

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


{

$answer = $result["answer"];
$q= 'q'.$result['id'];
if($_POST[$q] == $answer)
{
$score++;
}

}

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

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

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)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

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

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

}
echo "</table></p>";



}

?>


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 | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$q = $row["q"];
to

Code: Select all

$q = $_POST['q' . $row["id"]];
$$q :arrow: $q


maybe? :?
Post Reply