Php 制作';测验';-如何显示先前的答案?
我在做一种“测验”。用户被问到一个问题,可以在三个答案中选择。这是我的密码:Php 制作';测验';-如何显示先前的答案?,php,while-loop,Php,While Loop,我在做一种“测验”。用户被问到一个问题,可以在三个答案中选择。这是我的密码: // 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(
// 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循环的外部(和内部),但没有什么不同
有人知道吗?首先,定义一些函数来表示您试图在应用程序中执行的操作。(为了简单起见,我将不再讨论函数,而不再讨论OO原则) 理解应用程序功能的封装是良好软件设计的第一步 在您的情况下,视图层非常简单:
function displayQuestion($question, $answers) { /* display html for question and possible answers */ }
为了显示问题,您需要从数据库中获取问题及其答案。您可以这样做:
function getQuestion($questionNumber) { /* performs SQL queries and returns some representation of question, answers and correct answer */ }
然后你就有了真正的“业务”(游戏)逻辑本身,即:
function checkAnswer($questionNumber, $answerGiven) { /* returns true or false */ }
您还需要一些功能来控制整个游戏,例如:
function startNewGame() { /* reset internal variables */ }
function getCurrentQuestionNumber() { /* get the question that the user is up to */ }
function displayCurrentScore() { /* display html for current score */ }
最后一部分是将其全部连接在一起。(我的意思是讨论的最后一件事,而不是你做的最后一件事)。在这里,您可以将各个部分连接在一起,从HTTP请求开始,到模型和业务逻辑,再回到视图层,通过HTTP响应向用户显示页面。您可以将整个过程封装为一种方法:
function handleRequest() { /* perform the whole request-response cycle, calling the other methods as required */ }
请注意,以上只是对这些函数和它们的作用的一种广泛和相当普遍的方法,我不知道您的确切要求,因此会缺少一些部分。这也不是“最佳实践”,但它比将业务逻辑和数据库访问混合到HTML代码中要好:-)您能在一个块中向我们展示所有代码吗。这些代码段在文件中的顺序很重要。