0

我正在尝试使用 PHP 和 Mysql 进行测验。我已经创建了我的数据库并填充了它。数据库结构如下:question_id(PK),question, right answer, annswer1,answer2, answer3,level 和 test_id(FK)。

我创建了一个以随机顺序“绘制”问题的查询,并且我已经打乱了答案。用于向用户显示 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 ) )

我如何访问用户的答案以将它们与正确答案进行比较?

4

2 回答 2

2

好吧,既然您使用的是 PHP,您可以使用会话或 cookie。这可能不是最优雅的解决方案,但我会生成一个表示一系列问题的参考字符串并将其存储在一个会话或 cookie 变量中,然后再使用另一个会话或 cookie 变量来表示测验中的当前位置。此外,您可以使用其他变量来跟踪分数和其他花絮。然后当他们点击下一个按钮时,您只需更新变量并显示下一个问题,或者如果您已经达到它,则显示测验结束。

如果您想动态地执行此操作而不必加载另一个页面,那么您需要查看 javascript。

于 2012-11-25T08:40:04.423 回答
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";}
于 2012-11-25T08:54:22.260 回答