Home » Php » php – Relocation of option and answer key

php – Relocation of option and answer key

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to prepare questions for my students. There are questions in the school’s database. The table is as follows:

| id | qcontent                                                                     | o1  | o2   | o3  |
|----|------------------------------------------------------------------------------|-----|------|-----|
| 1  | Lorem Ipsum is simply dummy text of the printing and typesetting industry.   | ab  | ba   | ce  |
| 2  | Lorem Ipsum has been the industry's standard dummy text ever since the 1500s | ce  | sas  | de  |
| 3  | Contrary to popular belief, Lorem Ipsum is not simply random text.           | ase | sded | def |

The correct answers are the o1 option.

<?php


foreach($employee as $element) {

    echo $element->id . ". question: " .$element->qcontent . "<br>";  
    echo "A) " .$element->o1. "<br>";  
    echo "B) " .$element->o2. "<br>";  
    echo "C) " .$element->o3. "<br>";   
    echo "D) " .$element->o4. "<br>";   
    echo "E) " .$element->o5. "<br>";  

}

echo "<hr>";
echo "Answers:";

foreach($employee as $element) {

    echo $element->id. " - " .$element->o1. "<br>"; 

}

When I print the questions, all the right answers are option-A.
How can we generate the location of the questions and the answer key?

How to&Answers:

I would put the answers into an array and shuffle it. Then I would store the correct answer in an answer_key for output at the end.

<?php


foreach($employee as $element) {
    echo $element->id . ". question: " .$element->qcontent . "<br>"; 

    // put answers in an array
    $answers[0] = $element->01; 
    $answers[1] = $element->02;
    $answers[2] = $element->03;
    $answers[3] = $element->04;
    $answers[4] = $element->05;

    // randomize answers
    shuffle($answers);
    echo "A) " .$answers[0]. "<br>";  
    echo "B) " .$answers[1]. "<br>";  
    echo "C) " .$answers[2]. "<br>";   
    echo "D) " .$answers[3]. "<br>";   
    echo "E) " .$answers[4]. "<br>";  

   // add answer to answer key
   $answer_key[$element->id] = array_slice(array('A', 'B', 'C', 'D', 'E'), array_search($element->01, $answers), 1);
}


echo "<hr>";
echo "Answers:";

foreach($employee as $element) {

    // Output the correct answers
    echo $element->id. " - " .implode(answer_key[$element->id]). "<br>"; 

}

Answer:

By using shuffle() and making an array of all possible column of name:

<?php
// make array of all possible keys
$keys = ['o1', 'o2', 'o3', 'o4', 'o5'];
foreach($employee as $element) {
    // convert object to array
    $element = (array)$element;

    // shuffle it
    shuffle($keys);

    echo $element['id'] . ". question: " .$element['qcontent'] . "<br>";

    echo "A) " .$element[$keys[0]] . "<br>";  
    echo "B) " .$element[$keys[1]] . "<br>";  
    echo "C) " .$element[$keys[2]] . "<br>";   
    echo "D) " .$element[$keys[3]] . "<br>";   
    echo "E) " .$element[$keys[4]] . "<br>";  

}

echo "<hr>";
echo "Answers:";

foreach($employee as $element) {
    echo $element->id. " - " .$element->o1. "<br>";
}