【发布时间】:2013-01-01 03:31:33
【问题描述】:
我有一个小问题,我尝试进行在线测试,但我需要洗牌 answare 所以 1 是好的答案,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 进行随机排列,但我失败了。
【问题讨论】:
-
虽然
ORDER BY RAND()可以完成工作并且看起来不错,但它实际上是一个坏把戏:它无法扩展。更好的方法是选择 10 个随机数并将其用于您的LIMIT。 -
如果我不知道更好,我会说rowspan是魔鬼设计的属性!
标签: php arrays variables automated-tests shuffle