0

I have a database table with the following fields and an example:

QuestionID
Question
AnswerA
AnswerB
AnswerC
AnswerD
CorrectAnswer

Example:

QuestionID: 1
Question: What is 2 + 2?
AnswerA: 1
AnswerB: 2
AnswerC: 3
AnswerD: 4
CorrectAnswer: 4

If my database has say 20 questions, how would I get it so that not only do the questions appear in a random order but the answers appear on different radio buttons (To prevent the user from memorizing the location of the correct answer). This is to be integrated into a Facebook app. Below is my SQL query:

$sql="SELECT DISTINCT Question, QuestionID, AnswerA, AnswerB, AnswerC, AnswerD FROM miniproj ORDER BY rand() LIMIT 1";

This is the while statement I am using (I understand mysql_fetch_array is deprecated but I can sort that another time):

while ($myrow = mysql_fetch_array($result))
        {
            $answers = array($myrow['AnswerA'], $myrow['AnswerB'], $myrow['AnswerC'], $myrow['AnswerD']);
            shuffle($answers);
            echo("<form action='question.php' method='POST'>\n");
            echo("<h1>");
            echo("Q: ");
            echo($myrow['Question']);
            echo("</h1>");
            echo("<input type='radio' name='comments' value='A'>\n");
            echo($myrow['AnswerA']);
            echo("<p>");
            echo("<input type='radio' name='comments' value='B'>\n");
            echo($myrow['AnswerB']);
            echo("<p>");
            echo("<input type='radio' name='comments' value='C'>\n");
            echo($myrow['AnswerC']);
            echo("<p>");
            echo("<input type='radio' name='comments' value='D'>\n");
            echo($myrow['AnswerD']);
            echo("<p>");
            echo("<br />");
            echo("<input type='submit' value='Submit'>\n");
            echo("</form>\n");
        }

Any tips would be great

Dan
  • 1
  • 3

1 Answers1

1

U'll need to add in the beginning of PHP-script following function (thanks to Jon Stirling` s solution):

<?php
function shuffle_keys(&$array){
    $keys = array_keys($array);

    shuffle($keys);

    $result = array();

    foreach($keys as $key){ $result[$key] = $array[$key]; }

    $array = $result;
}
?>

Then, when you will process result set, I suggest you to do this:

<?php
while($myrow = mysql_fetch_array($result))
    {
        $answers = array(
            'A' => $myrow['AnswerA'],
            'B' => $myrow['AnswerB'],
            'C' => $myrow['AnswerC'],
            'D' => $myrow['AnswerD']
        );

        shuffle_keys($answers);

        echo "<form action='question.php' method='POST'>",
             PHP_EOL,
             "<h1>Q: {$myrow['Question']}</h1>";

        foreach($answers as $key => $value){
            echo "<p><input type='radio' name='comments' value='{$key}'>{$value}</p>";
        }

        echo "<input type='submit' value='Submit'>",
             PHP_EOL,
             "</form>",
             PHP_EOL;
    }
?>

P.S.: This topic suggests you to avoid usage of ***mysql_**** functions. It's old and deprecated extension. Use ***mysqli_**** or ***PDO_**** instead.

Community
  • 1
  • 1
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • Edited the original question to show what I have. Obviously I will have to remove the parts that say `echo($myrow['AnswerA']);` because this will stop the answers from being shuffled but not sure what to replace it with? – Dan Apr 19 '13 at 11:54
  • **@Dan**, looking through it. – BlitZ Apr 19 '13 at 11:57