Online Quiz

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
gabor
Forum Newbie
Posts: 5
Joined: Sat Jul 11, 2009 6:59 pm

Online Quiz

Post by gabor »

I'm creating an online quiz system using multiple choice questions. The student needs to choose the the correct answer from a dropdown menu created by this function:

Code: Select all

function options(){
$name="";
        $output="";
        $options["A"]="A";
        $options["B"]="B";
        $options["C"]="C";
        $options["D"]="D";
        $result=count($options);
        //print "Number of elements in array:&nbsp;".$result."<br>";
            echo '<select class="fill_form" name="choices">';
            echo '<option value="&nbsp;" selected=\"Selected\">Select</option>';
            
            foreach($options as $key => $value){
                echo "<option value=\"$key\">$value</option>\n";
            }
        echo '</select>';
}
The questions are generated by the following code:

Code: Select all

?>
<form id="test" name="test_form" method="post" action="ma_0030_0001_good.php">
<?php
$i=1;
while ($i<=3){
 
print $i.".&nbsp;";
//print "<br><b>Direct variable print:</b><br>";
            
            do{
                $a = rand(2,30);
                $exp1 = rand(2,10);
                $exp2 = rand(2,15);
                $answer1 = $exp1+$exp2;
                $answer2 = $exp2*$exp1;
                $answer3 = $exp1-$exp2;
                $answer4 = $exp2-$exp1;
            }while($answer1==$answer2||$answer1==$answer3||$answer1==$answer4||$answer2==$answer3||$answer2==$answer4||$answer3==$answer4);
 
 
        $question[$i.'a']=$a;
        $question[$i.'exp1']=$exp1;
        $question[$i.'exp2']=$exp2;
    
        $answer[$i.'answer1']=$answer1;
        $answer[$i.'answer2']=$answer2;
        $answer[$i.'answer3']=$answer3;
        $answer[$i.'answer4']=$answer4;
        
        $item=array('question'=>$question,'answer'=>$answer);
    
 
        print options(); //Answer choices drop down
//prnt question 
        print "&nbsp;&nbsp;&nbsp;&nbsp;".$a."<sup>".$exp1."</sup>&nbsp;x&nbsp;".$a."<sup>".$exp2."</sup>&nbsp;=\n\r<br><br>";
    //print answer choices
        print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>A.</b>&nbsp;&nbsp;&nbsp;".$a."<sup>".$answer[$i.'answer1']."</sup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"   ; 
        print "<b>B.</b>&nbsp;&nbsp;&nbsp;".$a."<sup>".$answer[$i.'answer2']."</sup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"     ;     
        print "<b>C.</b>&nbsp;&nbsp;&nbsp;".$a."<sup>".$answer[$i.'answer3']."</sup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"     ;     
        print "<b>D.</b>&nbsp;&nbsp;&nbsp;".$a."<sup>".$answer[$i.'answer4']."</sup><br>"     ; 
        
    print "<br><br>";
 
$i++;
 
}
    ?>
  <input type="submit" name="Submit" value="submit" />
  <input type="reset" name="Reset" value="Reset" />
</form>

When on ma_0030_0001_good.php I print the submitted array using print_r($_POST) I only see the last choice from the dropdown as most likely the answers owerwrite each other and only the last one is visible. How can I change the code to store the choices from the dropdown individually and how can I access them to see the answer for each question?

Thank you for your help.
Last edited by califdon on Sat Jul 11, 2009 7:37 pm, edited 1 time in total.
Reason: Edited by moderator to change from code=text to code=php
gabor
Forum Newbie
Posts: 5
Joined: Sat Jul 11, 2009 6:59 pm

Re: Online Quiz

Post by gabor »

Thank you for your comments. Is there a way to attach the question number to the <select> name "choices"? What can I use to replace &nbsp in formatting?
Thank you.
gabor
Forum Newbie
Posts: 5
Joined: Sat Jul 11, 2009 6:59 pm

Re: Online Quiz

Post by gabor »

Thank you
Post Reply