Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何以随机顺序显示内容?_Php_Mysqli - Fatal编程技术网

Php 如何以随机顺序显示内容?

Php 如何以随机顺序显示内容?,php,mysqli,Php,Mysqli,使用下面的代码,我将显示从数据库检索到的问题: $qandaquery = "SELECT q.QuestionId, q.QuestionNo, q.QuestionContent FROM Question q WHERE SessionId = ? GROUP BY q.QuestionId ORDER BY q.Quest

使用下面的代码,我将显示从数据库检索到的问题:

    $qandaquery = "SELECT q.QuestionId, q.QuestionNo, q.QuestionContent
                    FROM Question q
                    WHERE SessionId = ?
                    GROUP BY q.QuestionId
                    ORDER BY q.QuestionId";

    $qandaqrystmt=$mysqli->prepare($qandaquery);
    // You only need to call bind_param once
    $qandaqrystmt->bind_param("i",$session);
    // get result and assign variables (prefix with db)
    $qandaqrystmt->execute(); 
    $qandaqrystmt->bind_result($qandaQuestionId,$qandaQuestionNo,$qandaQuestionContent);

    $arrQuestionId = array();
    $arrQuestionNo = array();
    $arrQuestionContent = array();


    while ($qandaqrystmt->fetch()) {
    $arrQuestionId[ $qandaQuestionId ] = $qandaQuestionId;
    $arrQuestionNo[ $qandaQuestionId ] = $qandaQuestionNo;
    $arrQuestionContent[ $qandaQuestionId ] = $qandaQuestionContent;

  }

    $qandaqrystmt->close();


foreach ($arrQuestionId as $key=>$question) {

?>

<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"></span>:</strong></p>
<p><?php echo htmlspecialchars($arrQuestionNo[$key]) . ": " .  htmlspecialchars($arrQuestionContent[$key]); ?></p>
</div>


<?php

}

?>
我希望它以随机顺序显示问题。例如,如果我有以下3个问题:

QUESTION 1:

1. What is 2+2?

QUESTION 2:

2. What is 3+3?

QUESTION 3:

3. What is 4+4?
它可以按以下顺序显示,例如:

QUESTION 1:

2. What is 3+3?

QUESTION 2:

3. What is 4+4?

QUESTION 3:

1. What is 2+2?

使用
ORDER BY RAND()
函数将数组中元素的顺序随机化,而不是
ORDER BY q.QuestionId
使用
ORDER BY RAND()

范例

<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");

shuffle($my_array);
print_r($my_array);
?>

rray ( [0] => Cat [1] => Horse [2] => Dog )

rray([0]=>猫[1]=>马[2]=>狗)
mysql rand()函数如何

$qandaquery = "SELECT rand() as pos, q.QuestionId, q.QuestionNo, q.QuestionContent
                    FROM Question q
                    WHERE SessionId = ?
                    GROUP BY q.QuestionId
                    order by pos";

了解如何…使用此方法只需一个问题,我想在每个问题中添加一些复选框,这些复选框将是答案,答案无论如何都在查询中,正确答案和错误答案,如果我使用您的方法,这些复选框答案在随机出现时是否属于这些问题?这有意义吗?根据我给你的,这些行将被随机分配在一起。也就是说,所有列都将与给定的行匹配,只是行将被洗牌,所以看看它,我是否需要在代码中洗牌
$arrQuestionId
$qandaquery = "SELECT rand() as pos, q.QuestionId, q.QuestionNo, q.QuestionContent
                    FROM Question q
                    WHERE SessionId = ?
                    GROUP BY q.QuestionId
                    order by pos";