i need ur help guys.....
actually i m trying to obtain a score from a users radio button selection on a simple quiz. score calculation contains a problem....it won't give an accurate output to the browser. even i passed correct answers...but still it shows that 0 out of 5..
Code: Select all
<?php
// Connect to server and select database.
include('db_connect.php');
// Select quiz questions randomly
$quiz_query="select * from quiz order by rand() limit 5";
$quiz_result=mysql_query($quiz_query);
?>
<body>
<?php
// For hide the form after submit
if(!isset($_REQUEST['btnSubmit']))
{
?>
<form name="quiz" id="quiz" action="quiz.php" method="post">
<h1><u>Quiz</u></h1>
<?php
for($i=1; $i<=5; $i+=1)
{
$row=mysql_fetch_array($quiz_result);
$id=$row[0];
echo "$i ". $row[2]. "<br>";
echo " <input type='radio' name='a$id' value='$row[3]' />" .$row[3]. "<br>";
echo " <input type='radio' name='a$id' value='$row[4]' />" .$row[4]. "<br>";
echo " <input type='radio' name='a$id' value='$row[5]' />" .$row[5]. "<br>";
echo " <input type='radio' name='a$id' value='$row[6]' />" .$row[6]. "<br>";
echo "<br>";
}
?>
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<?php
}
// Display results
if(isset($_REQUEST['btnSubmit']))
{
$score=0;
$total=mysql_num_rows($quiz_result);
while ($result = mysql_fetch_array($quiz_result)){
$answer = $result[7];
$qid = $result[0];
if ($_POST['a$id'] == $answer){
$score++;
}
}
echo "<p align=center><b> Result </b></p>";
echo "<p align=center>$score/$total</p>";
$percent = number_format(($score * 100) / $total);
echo "<p align=center><b>$percent%</b></p>";
if($score == $total)
{
echo "<center>Congratulations! You got every question right!<br> You are eligible for the prize.</center>";
}
elseif($score >= 3)
{
echo "<center>Keep it up.</center>";
}
else
{
echo "<center>Fail</center>";
}
}
?>
</body>