我正在写一个测验。首先问题显示在 question.php 中,然后在 answer.php 中,对答案进行分析,如果答案正确,则将单词存储在数组中,如果错误则存储在另一个数组中。然后回到 question.php 等等。当没有更多问题时,result.php 将显示哪些单词是正确的,哪些是错误的。
请注意,每次加载 answer.php 时,会话数组都需要包含一个新词。
编辑:问题是会话数组只存储第一项
我的代码:
answer.php:
<?php
session_start();
$question = $_GET['question'];
$user_answer = $_GET['user_answer'];
$right_answer = mysql_query("SELECT french FROM words where english='$question'");
$fila = mysql_fetch_assoc($right_answer);
$fila_french = $fila['french'];
if ( $user_answer == $fila_french ) {
echo "Right";
$_SESSION['right_words'] = array();
array_push($_SESSION['right_words'], $question);
}
else { echo "Wrong";
$_SESSION['wrong_words'] = array();
array_push($_SESSION['wrong_words'], $question);
}
echo "<a href='question.php' > Next </a>";
?>
结果.php:
<?php
session_start();
$right_answers = implode(',',$_SESSION['right_words']);
$wrong_answers = implode(',',$_SESSION['wrong_words']);
echo "<h1> You finished the test </h1>
<p> You have these words right: $right_answers</p>
<p> You have these words wrong: $wrong_answers</p>
<a href='../exercises.php'> Go back to exercises </a>";
?>
谢谢你的帮助!