我正在尝试使用PHP和Mysql进行测验。我已经创建了我的数据库并填充了它。数据库构造如下:question_id(P.K),问题,正确答案,annswer1,answer2,answer3,level和test_id(F.K)。
我创建了一个以随机顺序“绘制”问题的查询,并且我已经改变了答案。用于向用户显示Qs和As的PHP脚本如下:
while ($index <= (count($test)-1)){
echo $test[$index]['question']. "<br/>";
$question_id=$test[$index]['question_id'];
$s_ans=User_Model::shuffle_answers($question_id);
foreach ($s_ans as $s_a){
echo "<input type='radio' name='test_ans[$index][$s_a]' id='$question_id' />$s_a <br />";
}
echo "<hr/>";
$index++;
}
以上代码会立即显示所有问题及其答案 我想做的,无法成功完成,是分别显示每个问题,当用户按下“下一步”按钮时,下一个问题及其答案将会显示给他。
所以,
a)有没有办法实现这个目标?
b)当我尝试访问用户的答案时,我得到以下输出:
Array ( [0] => Array ( [I like ice cream => on ) [1] => Array ( [i don't go to school] => on ) [2] => Array ( [i play basketball] => on ) [3] => Array ( [i like sailing] => on ) )
如何访问用户的答案以便将其与正确答案进行比较?
答案 0 :(得分:2)
好吧,既然你使用PHP就可以使用会话或cookie。它可能不是最优雅的解决方案,但我会生成一个代表一系列问题的参考字符串并将其存储在一个会话或cookie变量中,然后使用另一个会话或cookie变量来表示测验中的当前位置。此外,您可以使用其他变量来跟踪得分和其他花絮。然后,当他们点击下一个按钮时,您只需更新变量并显示下一个问题,或者如果已到达测验结束,则显示测验结束。
如果您想动态创建而无需加载其他页面,那么您需要查看javascript。
答案 1 :(得分:1)
多页测验非常容易。
A)这可以通过移除while()
循环来完成,但让计数器通过隐藏的<input>
或增加$_SESSION
变量
$index = $_POST['index'] // OR $_GET['index'] OR $_SESSION['index'] depending on your form method
echo $test[$index]['question']. "<br/>";
$question_id=$test[$index]['question_id'];
$s_ans=User_Model::shuffle_answers($question_id);
foreach ($s_ans as $s_a){
echo "<input type='radio' name='test_ans[$index][$s_a]' id='$question_id' />$s_a <br />";
}
if ($index < $total_questions){
$index++; // Increase var, and then use in hidden input
echo "<input type='hidden' name='index' value='$index' />";
echo "<input type='submit' name='submit_answer' value='Next' />";}
else {
echo "<input type='submit' name='submit_answer' value='You Are Done' />";}
//OR
$_SESSION['index'] = $index++;
B)没有你的代码,这只是一个例子 -
if ($user[$index]['their answer'] == $test[$index]['right answer'])
{ echo "You were correct";}
else
{ echo "Sorry, your answer was incorrect";}