PHP - 在随机测试中处理和比较答案

Morning:

This is my first question, i'll try to be as correct as I can. (And sorry about my English)

I'm trying to create a test with random Q/A taken from a date base. The structure of the DDBB is something like that:

preguntas -> id(int), pregunta (varchar)

respuestas-> id(int), pregid(int), respuesta(varchar), correcta(boolean/tinyint)

(Table names are in Spanish, sorry about the inconvenient)

The form code is this:

    <form  role="form" method="post"><!--ORDER BY RAND() LIMIT 0,10-->
    <?php 

            $recogepre = $pdo->query('SELECT * FROM preguntas ORDER BY RAND() LIMIT 0,10');
            while($row = $recogepre->fetch(PDO::FETCH_ASSOC) ){
                echo "<br><p>".$row['pregunta']."</p>";

                    $recogeres = $pdo->query("SELECT * FROM respuestas WHERE pregid='".$row["id"]."' ORDER BY RAND() LIMIT 0,30");
                    while ($row2 = $recogeres->fetch(PDO::FETCH_ASSOC)) {
                    echo "<input type=\"radio\" name=\"preg-".$row["id"]."\" value=\"".$row2["id"]."\"> ".$row2["respuesta"]."</br>";
                    }
                }

        ?>


    <br><br>
        <input type="submit" name="enviar" value="Enviar"> 
        <?php 
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        }

         ?>

    </form>

Code is working and it displays Q/A randomly. I would like to know how to compare the answers given by the user with the correct answer and change the location of the page to another than shows given answers and correct ones.

Thanks and sorry if I was rude...

<form  role="form" method="post"><!--ORDER BY RAND() LIMIT 0,10-->
    <?php 
            session_start();
            $_SESSION['questions'] = array();
            $_SESSION['answers'] = array();
            $recogepre = $pdo->query('SELECT * FROM preguntas ORDER BY RAND() LIMIT 0,10');
            $question_number = 1;
            while($row = $recogepre->fetch(PDO::FETCH_ASSOC) ){
                $_SESSION['questions'][$row['id']] = [
                    'question_id'=>$row['id'], 
                    'question'=>$row['pregunta'],
                ];
                echo "<br><p>".$row['pregunta']."</p>";

                $recogeres = $pdo->query("SELECT * FROM respuestas WHERE pregid='".$row["id"]."' ORDER BY RAND() LIMIT 0,30");
                while ($row2 = $recogeres->fetch(PDO::FETCH_ASSOC)) {
                    $_SESSION['answers'][$row2['id']] = [
                        'answer_id'=>$row2['id'], 
                        'question_id'=>$row['id'], 
                        'answer'=>$row2["respuesta"], 
                        'is_correct'=>$row2['correcta']
                    ];
                    echo "<input type=\"radio\" name=\"preg-".$question_number."\" value=\"".$row2["id"]."\"> ".$row2["respuesta"]."</br>"; //edit
                }
                $question_number++;
            }

        ?>


    <br><br>
        <input type="submit" name="enviar" value="Enviar"> 
        <?php 
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $answered_question_number = 1;
            $submitted_answer_name = "preg-".$answered_question_number;
            $feedback_answer_dataset = array();
            while($answer_id = $_POST[$submitted_answer_name]){
                $answer = $_SESSION['answers'][$answer_id];
                if($answer["is_correct"]){
                    $feedback_answer_dataset[] = [
                        "question"=>$_SESSION['questions'][$answer['question_id']],
                        "selected_answer"=>$answer["answer"], 
                        "correct_answer"=>$answer["answer"], 
                        "remark"=>"You chose the correct answer!"
                    ];
                } else {
                    $correct_answer_result = $pdo->query("SELECT * FROM respuestas WHERE pregid={$answer['question_id']} AND correcta=TRUE LIMIT 1");
                    $correct_answer = $correct_answer_result->fetch(PDO::FETCH_ASSOC))
                    $feedback_answer_dataset[] = [
                        "question"=>$_SESSION['questions'][$answer['question_id'],
                        "selected_answer"=>$answer["answer"], 
                        "correct_answer"=>$correct_answer["respuesta"], 
                        "remark"=>"You chose the wrong answer!"]
                    ];
                }
                $answered_question_number++;
                $answer_key = "preg-".$answered_question_number;
            }
        }

         ?>

    </form>

I have edited few lines from your code and added some code too.

What I have done is to give the questions a serial id in the form so that I can loop through it serially in the form processing code block. Also, I have stored the results from your query in the $_SESSION variable so that we don't have to run the same query again when processing.

In the form processing, each question is gotten from $_SESSION and checked for correctness, if its not correct, a correct answer having the same question id is retrieved from the DB. Everything is put into an array of associative arrays for each question. You can use that however you want in the html for your feedback page.

If you want the form processing to happen in a separate script, you can achieve that by including an "action" attribute in your tag.

I hope this works for you.