我正在做一个“测验”。用户会被问到一个问题,并且可以在三个答案之间进行选择。这是我的代码:
// query
$result = mysql_query("SELECT * FROM `db_game` LEFT JOIN `db_game_printers` ON (db_game.printerId = db_game_printers.id) ORDER BY RAND() LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
// explode answers
$row['printerQuestion'] = trim($row['printerQuestion']);
$question = explode(",", $row['printerQuestion']);
$printerQuestion = $row['title'];
$printerAnswer = $row['printerAnswer'];
// check answer
$answer = $row['printerAnswer'];
if (isset($_GET['answer'])) {
$answerUser = $_GET['answer'];
if ($answerUser == $answer) {
$correct = true;
} else {
$false = true;
}
}
它按预期工作。当用户通过单击答案来回答问题时,将显示下一个问题。我还想表明他们是否真的回答正确或不正确!我写了这段代码:
<div id="wrapper">
<div id="question">
<h1><?= $printerQuestion; ?></h1>
</div><!-- /question -->
<ul id="answers">
<li style="text-align:left;"><a href="index.php?answer=<?=$question[0];?>"><button class="punch"><?=$question[0];?></button></a></li>
<li style="text-align:center;"><a href="index.php?answer=<?=$question[1];?>"><button class="punch"><?=$question[1];?></button></a></li>
<li style="text-align:right;"><a href="index.php?answer=<?=$question[2];?>"><button class="punch"><?=$question[2];?></button></a></li>
</ul>
<div class="clear" style="margin-bottom:50px;"></div>
<?php
$prev_printerQuestion = $printerQuestion;
$prev_printerAnswer = $printerAnswer;
} // end while loop
?>
<?php
if (isset($_GET['answer'])) {
if ($correct) {
echo "Correct! The ".$prev_printerQuestion." uses the ".$prev_printerAnswer."";
}
else if ($false) {
echo "Not correct. ".$prev_printerQuestion." = ".$prev_printerAnswer."" ;
}
}
?>
</div><!-- /wrapper -->
检查答案是否正确使用当前数据。不是来自上一个问题。我尝试将这些值放在一个单独的变量中并在 while 循环之外(和内部),但这没有任何区别。
有人知道吗?