随机单选按钮PHP

时间:2015-04-09 12:32:43

标签: php random radio-button

我有这些单选按钮......

<div class="radio"><label><input type="radio" name="1" id="1" value="0"><?php echo $incorrect1; ?></label></div>
<div class="radio"><label><input type="radio" name="2" id="2" value="0"><?php echo $incorrect2; ?></label></div>
<div class="radio"><label><input type="radio" name="3" id="3" value="1"><?php echo $correct; ?></label></div>  

我喜欢他们随机显示不同的顺序,我在php中如何做到这一点?感谢

PHP

$object2 = new ConnectToDB();
$result2 = $object2->getQue($ksGet,$contIDGet);

foreach($result2 as $row){

    $question .= "" . $row['question'] . ""; 
    $incorrect1 .= "" . $row['incorrect1'] . "";
    $incorrect2 .= "" . $row['incorrect2'] . "";
    $correct .= "" . $row['correct'] . ""; 

}

2 个答案:

答案 0 :(得分:2)

虽然时间很长,但我希望通过这个答案来教导randif else如何对我的团队起作用:p

第1步:

我将三个单选框声明为3变量,名为$first$second$third

第2步:

生成1到3 $case = rand(1,3);

之间的随机数

第3步:

将其置于if-else案例

<?php

$first = '<div class="radio"><label><input type="radio" name="1" id="1" value="0"><?php echo $incorrect1; ?></label></div>';
$second = '<div class="radio"><label><input type="radio" name="2" id="2" value="0"><?php echo $incorrect2; ?></label></div>';
$third = '<div class="radio"><label><input type="radio" name="3" id="3" value="1"><?php echo $correct; ?></label></div>';
$case = rand(1, 3);
if ($case == 1) {
    echo $first;
    echo $second;
    echo $third;
} elseif ($case == 2) {
    echo $second;
    echo $first;
    echo $third;
} else {
    echo $third;
    echo $first;
    echo $second;
}
?>

答案 1 :(得分:0)

$answer1=\"<div class=\"radio\"><label><input type=\"radio\" name=\"1\" id=\"1\" value=\"0\">$incorrect1</label></div>
";
$answer2="<div class=\"radio\"><label><input type=\"radio\" name=\"2\" id=\"2\" value=\"0\">$incorrect2</label></div>
";
$answer3="<div class=\"radio\"><label><input type=\"radio\" name=\"3\" id=\"3\" value=\"1\">$correct</label></div>
";
$rand=rand(1,3);
switch($rand){
case 1:
$answers.=$answer3.$answer1.$answer2;
break;
case 2:
$answers.=$answer2.$answer3.$answer1;
break;
case 3:
$answers.=$answer1.$answer2.$answer3;
break;
}