0

以下のコードで、データベースから取得した質問を表示しています。

    $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

}

?>

今それが言うところQUESTION

<p><strong>QUESTION # <?php echo $q; ?> :</strong></p> <?php $q++; ?>

同じ場所にとどまりたい

ただし、質問の詳細が表示される場所:

<p><?php echo htmlspecialchars($arrQuestionNo[$key]) . ": " .  htmlspecialchars($arrQuestionContent[$key]); ?></p>

質問をランダムな順序で表示したい。たとえば、次の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?
4

3 に答える 3

0

ORDER BY q.QuestionId使用する代わりにORDER BY RAND()

于 2013-02-07T04:49:24.267 に答える
0

このshuffle()関数は、配列内の要素の順序をランダム化します。続きを読む

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

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

rray ( [0] => Cat [1] => Horse [2] => Dog )
于 2013-02-07T04:50:28.257 に答える
0

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";
于 2013-02-07T04:56:19.360 に答える