对于多项选择测验应用程序,我想用正确答案显示虚拟答案。但正确答案在每个不同的问题上处于不同的位置。
这是我尝试过的,但它似乎没有起作用:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$dummy_num = 1;
foreach ($query->result() as $row)
{
$rand_number = rand(1, 3);
if ($dummy_num == $rand_number)
{
$dummy_num = $rand_number + 2;
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
//Now echo the real answer
echo '<h4>Answer '.$rand_number.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>'; //Get id's for each.echo $row->id;
}
else
{
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
$dummy_num++;
}
}
echo '</div>';
echo ' <hr/>';
}
?>
答案 0 :(得分:1)
您应该使用shuffle function。 在你的情况下,它将是:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$answers=$query->result();
shuffle($answers);
foreach ($answers as $nr=>$row)
{
echo '<h4>Answer '.($nr+1).'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
}
echo '</div>';
echo ' <hr/>';
}
?>
答案 1 :(得分:0)
使用shuffle
将答案放在数组中$random_array = shuffle($answers);
答案 2 :(得分:0)
您可以将结果放入数组(1个正确,3个不正确),然后shuffle
,然后输出它们?
$answers = array();
array_push($answers, "answer1");
array_push($answers, "answer2");
array_push($answers, "answer3");
array_push($answers, "answer4");
shuffle($answers);
foreach ($answers as $answer) {
echo $answer;
}
答案 3 :(得分:0)
您需要做的就是将答案的键放在一个数组中并拨打shuffle()
。像这样:
$keys = array_keys($answers);
shuffle($keys);
for ($key in $keys) {
echo $answers[$key];
}
答案 4 :(得分:0)
我建议将所有答案放入一个数组中,然后使用shuffle()
函数将它们随机化。一旦它们被洗牌,只需用循环迭代数组并构建标记。