quiz score

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
scenic
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2008 4:19 am

quiz score

Post by scenic »

Hello.....
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 &nbsp;&nbsp;". $row[2]. "<br>"; 
                echo "&nbsp;&nbsp;<input type='radio' name='a$id' value='$row[3]' />" .$row[3]. "<br>";
                echo "&nbsp;&nbsp;<input type='radio' name='a$id' value='$row[4]' />" .$row[4]. "<br>";
                echo "&nbsp;&nbsp;<input type='radio' name='a$id' value='$row[5]' />" .$row[5]. "<br>";
                echo "&nbsp;&nbsp;<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>
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: quiz score

Post by papa »

Here's your error:
if ($_POST['a$id'] == $answer)
scenic
Forum Newbie
Posts: 7
Joined: Mon Dec 01, 2008 4:19 am

Re: quiz score

Post by scenic »

thank you for reply sir....

but plz correct it if u can.......
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: quiz score

Post by papa »

if ($_POST['a$qid'] == $answer)

Might do it.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: quiz score

Post by aceconcepts »

Or

Code: Select all

$_POST["a$qid"]
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: quiz score

Post by jaoudestudios »

$_POST['a$id']
It would be more efficient and for future coding as...

Code: Select all

 
$_POST['a'.$id]
 
NB: I recommend not to use $_REQUEST
Post Reply