随机单选按钮 PHP
Random radio buttons PHP
我有这些单选按钮...
<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'] . "";
}
$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;
}
虽然很长,但我想做这个答案来教我的团队 rand
和 if 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;
}
?>
我有这些单选按钮...
<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'] . "";
}
$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;
}
虽然很长,但我想做这个答案来教我的团队 rand
和 if 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;
}
?>