我有一点问题,我尝试进行在线测试,但我需要对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>';
我尝试了一些数组shuffle $ answare_good&amp; $ answare_badi&amp; $ answare_badii但是我很想。
答案 0 :(得分: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
就可以了。
答案 1 :(得分:1)
如果你需要提供变量,你可以另外提取shuffle
$answers = array(
'opt1' => 'answer1',
'opt2' => 'answer2',
'opt3' => 'answer3'
);
shuffle($answers);
extract($answers);
//访问变量
echo $opt1;
echo $opt2;
echo $opt3;