Search code examples
phprandommultiple-choice

check answer with the random mc questions from database


I was going to display some random questions called from my database and check answers using the code:

question.php:

<html>
<body>
<h3>Please answer all questions.</h3><br><br>
<form action="phpinfo.php" method="post">
<?php
//connect to db
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("mc", $server);
$question = mysql_query("SELECT * FROM `question` ORDER BY RAND() LIMIT 20;"); // 60 questions in my database
$x = 0;
while ($row = mysql_fetch_array($question))
{   
   echo "Question: ".$row['q_do'] . '<br />'; //q_do are my questions
   echo '<input type="radio" name="a'.$x.'" value=a />' ,"A ".$row['a'] . '<br />'; // answer a
   echo '<input type="radio" name="a'.$x.'" value=b />' ,"B ".$row['b'] . '<br />'; // answer b
   echo '<input type="radio" name="a'.$x.'" value=c />' ,"C ".$row['c'] . '<br />'; // answer c
   echo '<input type="radio" name="a'.$x.'" value=d />' ,"D ".$row['d'] . '<br />'; // answer d
   $x++;   
   };
mysql_close($server);
?>
<input type="submit" name="Submit" value="Submit" /> //go to next page
<br>
</form>
</body>
</html>

phpinfo.php:

<html>
<body>
<?php
//connect to db
$server = mysql_connect ('localhost', 'username', 'password');
mysql_select_db("mc", $server);
$question = mysql_query("SELECT * FROM `question` ;");
$x = 0;
$score = 0;
while ($row = mysql_fetch_array($question)) //do not know how to call the questions did in previous page since they are random
{
    echo $row['q_do'] . '<br />';

    $answered = $row[$_POST['a'.$x]] ; // the answers
    $correct = $row['corr_ans'] ; 

    if ($answered == $correct ) {
        $score++;
        $acolor = 'green' ;
    }
    else {
    $acolor = 'red' ;
    }
    echo 'you answered <font color=' . $acolor . '>' . $answered . '<font color=black> <br />';
    echo 'the correct answer was ' . $correct . '<br />' ;
    echo '-------------------------------------- <br />' ;
    $x++;
}
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
mysql_close($server);
?>
</body>
</html>

How can I change the above codes to make the whole system work? These codes work where there are no random and the questions followed by ascending order.


Solution

  • The easiest way to solve your problem is to simply append the id of the question to the value attribute of each question like this:

       echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />';
    

    This will give you a unique answer value for each question which you can use to get the id of the question and then pull the question from the database.

    So if your value for one of the answers is 'b7' you know that the user answered question 7 with option b.

    To get that id from the string like 'b7' you could use the following:

    $questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT);
    

    Because now you know what the question is, you can get it from the database using the id and do the rest of processing for that question.

    So on your server side script, you would have to change your loop to simply iterate 20 times (because thats how many questions have been submitted). This is how your both files should look after you make all the required changes:

    question.php:

    <html>
    <body>
    <h3>Please answer all questions.</h3><br><br>
    <form action="phpinfo.php" method="post">
    <?php
    //connect to db
    $server = mysql_connect ('localhost', 'username', 'password');
    mysql_select_db("mc", $server);
    $question = mysql_query("SELECT * FROM `question` ORDER BY RAND() LIMIT 20;"); // 60 questions in my database
    $x = 0;
    while ($row = mysql_fetch_array($question))
    {   
       echo "Question: ".$row['q_do'] . '<br />'; //q_do are my questions
       echo '<input type="radio" name="a'.$x.'" value="a'.$row['id'].'" />' ,"A ".$row['a'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value="b'.$row['id'].'" />' ,"B ".$row['b'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value="c'.$row['id'].'" />' ,"C ".$row['c'] . '<br />';
       echo '<input type="radio" name="a'.$x.'" value="d'.$row['id'].'" />' ,"D ".$row['d'] . '<br />';
       $x++;   
       };
    mysql_close($server);
    ?>
    <input type="submit" name="Submit" value="Submit" /> //go to next page
    <br>
    </form>
    </body>
    </html>
    

    phpinfo.php:

    <html>
    <body>
    <?php
    //connect to db
    $server = mysql_connect ('localhost', 'username', 'password');
    mysql_select_db("mc", $server);
    $x = 0;
    $score = 0;
    
    // Make 20 iterations to process the 20 answers
    for ($i=0; $i<=19; $i++)
    {
        $fullAnswer = $row[$_POST['a'.$x]] ;
    
        // Find the question id
        $questionId = filter_var($fullAnswer, FILTER_SANITIZE_NUMBER_INT);
    
        // Find the answer
        $answer = substr($fullAnswer, 0, 1);
    
        // get the question for the answer of current iteration
        $result = mysql_query("SELECT * FROM `question` WHERE id=$questionId LIMIT 1;");
        $question = mysql_fetch_assoc($result);
    
        echo $question['q_do'] . '<br />';
    
        $correct = $question['corr_ans'] ; 
    
        if ($answer == $correct ) {
            $score++;
            $acolor = 'green' ;
        }
        else {
        $acolor = 'red' ;
        }
        echo 'you answered <font color=' . $acolor . '>' . $answer . '<font color=black> <br />';
        echo 'the correct answer was ' . $correct . '<br />' ;
        echo '-------------------------------------- <br />' ;
        $x++;
    }
    echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
    mysql_close($server);
    ?>
    </body>
    </html>
    

    Simple :)

    This could should work but I didn't test.

    Just so that you know, using mysql_query is a bad idea. It is depreciated. You should use PDO instead. Look up PHP docs of how to use it and then change all database stuff to use it in your code.

    Good luck.