1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP - quiz scoring script

Discussion in 'Scripts' started by steveallen, Jul 27, 2006.

  1. #1
    Hi all,

    Can anyone offer assistance with the following:

    I am trying to obtain a score from a users radio button selection on a simple quiz.

    I used the print_r($_POST); to test if the form is passing the correct info and that seems to be fine. But the score calculation section falls apart - keeps saying you scored 0 out of 3 (even when the right answers have been passed by the form).

    Any ideas?

    Thanks Steve

    
    <?php
    
    include("contentdb.php");
    
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
    	
    if (!$_POST['submit']) {
    
    
    	echo "<form method=post action=$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 = $result["q"];
    			
    		if ($$q == $answer) {
    		
    		$score++; 
    		
    		}
    		}
    	
    	echo "<p align=center><b>You scored $score out of $total</b></p>";
    	echo "<p>";
    	
    	}
    
    
    ?>
    PHP:
     
    steveallen, Jul 27, 2006 IP
  2. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #2
    
    <?php
    include("contentdb.php");
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
    
    if (!$_POST['submit']) {
    echo "<form method=post action=$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"];
    
    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_numrows($display);
    
    while ($result = mysql_fetch_array($display)) {
    
    $answer = $result["answer"];
    $id = $result["id"];
    
    if ($_POST[q$id] == $answer) {
    $score++;
    }
    }
    
    echo "<p align=center><b>You scored $score out of $total</b></p>";
    echo "<p>";
    
    }
    
    
    ?>
    
    Code (markup):
     
    PinoyIto, Jul 27, 2006 IP
  3. steveallen

    steveallen Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your answer.

    I tried implementing your code, but the scoring section still seems to contain a problem. It won't output anything to the browser ...

    Anything else you could suggest?

    Thanks anyway

    Steve
     
    steveallen, Jul 27, 2006 IP
  4. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #4
    Can you point me to the url of your script I want to check something..
     
    PinoyIto, Jul 27, 2006 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    On line 44:
    if ($$q == $answer) {

    is that double dollar sign a typo? That shouldn't be there I would guess, and could very well be the problem...
     
    TwistMyArm, Jul 27, 2006 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ahh... I'm an idiot. Sorry.

    I would say that PinoyIto was on the right track, but instead of:
    if ($_POST[q$id] == $answer) {

    I'd do something more like:
    if ($_POST[ 'q' . $id] == $answer) {


    Anyway, good luck with it all...
     
    TwistMyArm, Jul 27, 2006 IP
  7. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hmm... I have a small suggestion
    Try quoting the name here and in the other checkboxes
    <input type=radio name=q$id value=\"$opt1\">

    to
    <input type=\"radio\" name=\"q$id\" value=\"$opt3\">

    I cant find anything wrong with the script now. But I will keep looking.

    Thomas
     
    coderlinks, Jul 27, 2006 IP
  8. steveallen

    steveallen Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi PinoyIto,

    Here is the url to my small test quix script sample:

    http://dev.imaging4.co.nz/athleticsQuiz/pages/admin/quizv1.0/quiz1.php

    I have included a print_r(['$_POST']) at the end to see what info the form was passing itself. That's why the array info is displayed ..

    Cheers

    Steve
     
    steveallen, Jul 27, 2006 IP
  9. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #9
    I tried this script in my server and it works

    <?php 
    
    include("contentdb.php"); 
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);    
    
    if (!$_POST['submit']) { 
    
     echo "<form method=post action=$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]";                    
    $q = trim($q);
    if ($_POST[$q] == $answer) {                
    $score++;                 
    }        
    }        
    
    echo "<p align=center><b>You scored $score out of $total</b></p>";    
    echo "<p>";        
    }  
    ?> 
    Code (markup):
     
    PinoyIto, Jul 27, 2006 IP
    poseidon likes this.
  10. steveallen

    steveallen Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks for that script suggestion! - Works great - awesome!!!!

    Steve
     
    steveallen, Jul 28, 2006 IP
  11. mbu_bashir

    mbu_bashir Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    if I use this:
    $display = mysql_query("SELECT * FROM $table ORDER BY RAND() LIMIT 4",$db);
    instead of the following:
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);

    then is not come correct answer ..
    But why Please give answer to me......
     
    mbu_bashir, Jan 9, 2011 IP
  12. jewel1989r

    jewel1989r Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks for your good & effective script.. I use this script and its works good... here is my links where i used that script.. varsitycampus.com/bcs/test.php

    Thanks
     
    jewel1989r, Sep 27, 2011 IP
  13. pogopatterson

    pogopatterson Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    I'm delighted to say the last script entered worked perfectly for me, once I changed my background table to the structure the author used. But there was much in the script that confused me, and I don't like simply copying others work without understanding it implicitly. Don't want to post my questions just yet, since the author of the script may no longer be taking questions for what is a fairly old script. I'll wait to see if there is anyone out there who had contributed to this thread previously and is still picking up their e-mails to the thread before posting further questions. Thanks.
     
    pogopatterson, Aug 6, 2015 IP