1

我有一个小问题,我尝试进行在线测试,但我需要洗牌 answare 所以 1 是好的 answare 和 2,3 坏的但一直出现 a = 1 b = 2 c = 3 我想要一些随机化的东西a=2 b=3 c=1 我该怎么做?

代码在这里:

$result = mysql_query( "SELECT * FROM questions ORDER BY RAND() LIMIT 0,10");
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
    $question_id = $row['question_id'];
    $question = $row['question'];
    $answare_good = $row['answare_good'];
    $answare_badi = $row['answare_badi'];
    $answare_badii = $row['answare_badii'];
    $number += 1;
    echo '<tr>
    <td rowspan="3" width="10">'.$number.'</td>
    <td rowspan="3" width="70%"><p>'.$question.'</p></td>
    <td width="30%" height="33%"><input type="checkbox" name="a1" id="a1">
    '.$answare_good.'</td>
    </tr>
    <tr>
    <td width="30%" height="33%"><input type="checkbox" name="a2" id="a2">
    '.$answare_badi.'</td>
    </tr>
    <tr>
    <td  width="30%" height="33%"><input type="checkbox" name="a3" id="a3">
    '.$answare_badii.'</td>
    </tr>';

我尝试了一些东西来排列洗牌 $answare_good & $answare_badi & $answare_badii 但我失败了。

4

2 回答 2

2

这段代码怎么样:

$result = mysql_query("SELECT * FROM questions ORDER BY RAND() LIMIT 0,10");
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
    $question_id = $row['question_id'];

    $answers = array(
        array('a1', 'val1', $row['answare_good']),
        array('a2', 'val2', $row['answare_badi']),
        array('a3', 'val3', $row['answare_badii']),
    );
    shuffle($answers);

    $number += 1;

    echo '<tr>
    <td rowspan="3" width="10">'.$number.'</td>
    <td rowspan="3" width="70%"><p>'.$row['question'].'</p></td></tr>';
    foreach($answers as $answer) {
        echo '<tr><td width="30%" height="33%"><input type="checkbox" name="'.$answer[0].'" id="'.$question_id.'-'.$answer[0].'" value="'.$answer[1].'" />'.$answer[2].'</td></tr>';
    }
}

您还必须指定复选框的值才能获得选中答案的值...

也移动到PDO或至少mysqli_*作为mysql_*功能现在已弃用的功能......

编辑:还考虑使用radio而不是checkbox- 假设用户应该只选择一个答案。如果您想让用户选择一个以上(或所有三个答案),那么checkbox就可以了。

于 2013-01-17T15:04:05.583 回答
1

如果您需要提供可用的变量,您可以在随机播放中额外提取

$answers = array(
 'opt1' => 'answer1',
 'opt2' => 'answer2',
 'opt3' => 'answer3'
);

shuffle($answers);
extract($answers);

// 访问变量

echo $opt1;
echo $opt2;
echo $opt3;
于 2013-01-17T14:58:13.673 回答