简体   繁体   中英

Shuffle a MySQL row in a PHP query

I am doing this in PHP:

function populateQuestions(){
     global $dbc;
     $query = "SELECT * FROM Questions";

     $result = $dbc->getAll($query);
     shuffle($result);
     return $result;
}

The above snippet shuffles the questions that my user sees, so he sees random questions every time.

However my SQL table has answerA,answerB,answerC,answerD as possible answer fields. answerA is always correct.

I would like to shuffle these 4 fields ONLY before I json_decode them.

So answerA might be answer B, answer C might be answer A etc etc...

Is that possible to do?

My table structure for table Questions:

ID - name - image - answerA - answerB - answerC - answerD - category

Try:

SELECT id, answer FROM(
    SELECT id, answerA as answer FROM Question UNION ALL
    SELECT id, answerB FROM Question UNION ALL
    SELECT id, answerC FROM Question UNION ALL
    SELECT id, answerD FROM Question)
ORDER BY id, RAND();

When you display the data, just shufle an array and print the questions based on that array:

$answerOrder = shuffle(array('answerA', 'answerB', 'answerC', 'answerD'));
foreach($answerOrder as $columnName) {
    echo $questions[$currentId][$columnName]
}

Assuming that the $questions array is the return value of the populateQuestions() and you iterate throught it. In the iteration the $currentId is the key from the $questions array.

The $questions[$currentId] contains a row from the db.

With this solution, you do not add extra in the DB and you can track the correct answers position while you are displaying it.

This is but a simple approach to how I interpreted your question.

<?php
    function populateQuestions(){
        //global $dbc;
        //$query = "SELECT * FROM Questions";
        // I'm assuming that your array from the database looks like the one represented by $result.
        $result = array(array('ID'=>'1', 'name'=>'Question One', 'image'=>'1.jpg', 'answerA'=>'ans1', 'answerB'=>'ans2', 'answerC'=>'ans3', 'answerD'=>'ans4', 'category'=>'quiz'), array('ID'=>'2', 'name'=>'Question Two', 'image'=>'2.jpg', 'answerA'=>'ans5', 'answerB'=>'ans6', 'answerC'=>'ans7', 'answerD'=>'ans8', 'category'=>'quiz')); /// sample database results array
        $temporaryArray = array();
        foreach($result as $key=>$value){
            $letters = range('A', 'D');
            shuffle($letters);
            $temporaryArray[$key]['answer'.$letters[0]] = $value['answerA'];
            $temporaryArray[$key]['answer'.$letters[1]] = $value['answerB'];
            $temporaryArray[$key]['answer'.$letters[2]] = $value['answerC'];
            $temporaryArray[$key]['answer'.$letters[3]] = $value['answerD'];
        }
        shuffle($temporaryArray);
        return $temporaryArray;
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM