如何将问题与数据库中的相应答案随机化


how to randomise questions with corresponding answers from the database

我一直在制作一个智力竞赛应用程序,有一次我陷入了困境。我正在使用PHP和MySQL来制作它。所以,我现在想从数据库中随机检索问题。但是,当我尝试在问题中使用rand()时,它的选择与它应该做的不同。我应该做什么将问题的随机性与答案同步。

class Quiz {
protected $_db;
protected $_answers = array();
protected $_questions = array();
protected $_question;
protected $_users;
protected $_leaderboard;
protected $_currentuser;
public $session;
public function __construct('Pimple $container)
{
    $this->_currentuser = $container['user'];
    $this->session = $container['session'];
    $this->_leaderboard = $container['leaderboard'];
    $this->_users = $this->_leaderboard->getMembers();
    try
    {
        //$this->_db = new PDO('mysql:host='.Config::$dbhost.';dbname='.Config::$dbname,  Config::$dbuser,  Config::$dbpassword);
        $this->_db = $container['db'];
        $this->_populateQuestions();
    }
    catch ('PDOException $e)
    {
        return $e;
    }
}
public function getAnswers($questionid = false)
{   
    if ($questionid)
    {
        //pull answers from db for only this question
        $answersql = "SELECT text FROM answers where question_id = :id ORDER BY correct DESC";
        $stmt = $this->_db->prepare($answersql);
        $stmt->bindParam(':id', $questionid, 'PDO::PARAM_INT);
        $stmt->execute();
        while ($result = $stmt->fetchObject())
        {
           array_push($this->_answers,$result->text);
        }
    }
    else
    {
        //pull all answers from db grouped by question
        $answersql = "SELECT group_concat( a.text ORDER BY a.correct DESC SEPARATOR '~' ) FROM answers a GROUP BY a.question_id";
        $stmt = $this->_db->query($answersql);
        $stmt->execute();
        $resultset = $stmt->fetchAll('PDO::FETCH_NUM);
        foreach ($resultset as $csv)
        {   
            $tmparray = explode('~', $csv[0]);
            array_push($this->_answers,$tmparray);
        }
    }
    return $this->_answers;
}

public function getQuestion($questionid) 
{
    $questionsql = "select text from questions where id = :id order by rand()";
    $stmt = $this->_db->prepare($questionsql);
    $stmt->bindParam(':id', $questionid, 'PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetchObject();
    $this->_question = $row->text;
    return $this->_question;
}
public function getQuestions()
{
    return $this->_questions;
}
private function _populateQuestions() 
{
    $questionsql = "select text from questions order by id asc";
    $stmt = $this->_db->query($questionsql);
    $stmt->execute();
    while ($row = $stmt->fetchObject())
    {
        $this->_questions[] .= $row->text;
    }
}

将一个与问答数组长度相同的数组随机化,将值作为数组索引,然后迭代测试问答并更改其键。每个订单都是一样的。这里有一个例子:

$order = [1, 0];
$ques = ['foo', 'bar'];
$ans = ['baz', 'bop'];
$new_ques = [];
$new_ans = [];
foreach($order as $k=>$v){
    $new_ques[$v] = $ques[$k];
    $new_ans[$v] = $ans[$k];
}
ksort($new_ques);ksort($new_ans);
var_dump($new_ques, $new_ans);

这有点低效,因为它创建了一个新的数组来放置值,然后对它们进行排序,但它应该能满足您的需要。