带有屏幕结果的 PHP 测验
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15482190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP quiz with on screen results
提问by C B
I am trying to create a simple quiz based on a tutorial I found. http://css-tricks.com/building-a-simple-quiz/
我正在尝试根据我找到的教程创建一个简单的测验。http://css-tricks.com/building-a-simple-quiz/
Unfortunately, this is making me spin my wheels and the answer is probably pretty simple.
不幸的是,这让我转动轮子,答案可能很简单。
I got this working perfectly. I would like to change the functionality. Instead of it counting the answers, I want it to do something else.
我让这个工作完美。我想更改功能。与其计算答案,我希望它做其他事情。
- I would like to display the questions again.
- Also, with the chosen answer and the correct answer. echo the answer, notthe letter A,B,C,D.
- 我想再次显示问题。
- 此外,还有选择的答案和正确的答案。回应答案,而不是字母 A、B、C、D。
It seems silly to me to have a quiz and say you missed 2 and not show what questions were missed.
在我看来,进行测验并说您错过了 2 道而不显示错过了哪些问题似乎很愚蠢。
I'd prefer to avoid use of a database. It can be on screen, on a new screen or e-mailed. No preference. Any suggestions?
我宁愿避免使用数据库。它可以在屏幕上、在新屏幕上或通过电子邮件发送。没有偏好。有什么建议?
Here is the code from the above mentioned site:
这是来自上述网站的代码:
<form action="grade.php" method="post" id="quiz">
<li>
<h3>CSS Stands for...</h3>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A) Computer Styled Sections </label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B">B) Cascading Style Sheets</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C">C) Crazy Solid Shapes</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D">D) None of the above</label>
</div>
</li>
</form>
<input type="submit" value="Submit Quiz" />
Then the PHP script:
然后是PHP脚本:
<?php
$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];
$answer4 = $_POST['question-4-answers'];
$answer5 = $_POST['question-5-answers'];
$totalCorrect = 0;
if ($answer1 == "B") { $totalCorrect++; }
if ($answer2 == "A") { $totalCorrect++; }
if ($answer3 == "C") { $totalCorrect++; }
if ($answer4 == "D") { $totalCorrect++; }
if ($answer5) { $totalCorrect++; }
echo "<div id='results'>$totalCorrect / 5 correct</div>";
?>
Any suggestions or links would be much appreciated. My Google skills are failing me. Everything I think to search for brings up irrelevant stuff.
任何建议或链接将不胜感激。我的谷歌技能让我失望。我想搜索的所有东西都会带来不相关的东西。
回答by Gilly
To be able to echo the answer, and not the letter you need to store the question first. You don't need to use a database, you can just use an array.
为了能够回应答案,而不是你需要先存储问题的字母。您不需要使用数据库,只需使用数组即可。
If your going to use arrays, I'd suggest storing everything in an array. Since the structure of the html is just the same, this can save you so much time. You could just write a question once and implement it automatically throughout the script.
如果您要使用数组,我建议将所有内容都存储在一个数组中。由于 html 的结构是一样的,这可以为您节省很多时间。您可以只写一个问题,然后在整个脚本中自动实现它。
<?php
$Questions = array(
1 => array(
'Question' => 'CSS stands for',
'Answers' => array(
'A' => 'Computer Styled Sections',
'B' => 'Cascading Style Sheets',
'C' => 'Crazy Solid Shapes'
),
'CorrectAnswer' => 'A'
),
2 => array(
'Question' => 'Second question',
'Answers' => array(
'A' => 'First answer of Second question',
'B' => 'Second answer Second question',
'C' => 'Third answer Second question'
),
'CorrectAnswer' => 'C'
)
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo '<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
} else {
echo '<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; // Replace style with a class
}
echo '<br /><hr>';
}
} else {
?>
<form action="grade.php" method="post" id="quiz">
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<li>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php } ?>
</li>
<?php } ?>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>
The cool thing about this is that you don't need to add any HTML or anything if you want to add another question. Just add the question with its answers, the correct answer and it automatically works! By the way, this is one file, not 2. So it should submit to itself.
很酷的一点是,如果您想添加另一个问题,则无需添加任何 HTML 或任何内容。只需添加问题及其答案,正确答案,它就会自动工作!顺便说一下,这是一个文件,而不是 2 个。所以它应该提交给自己。
回答by Marc B
The basic structure would be something like
基本结构类似于
if ($answer1 == "B") {
$totalCorrect++;
} else {
$wronganswers[] = "You got #1 wrong. correct answer is B / ...text_of_answer_here ";
}
...
if ($totalCorrect != $number_of_questions) {
echo implode($wronganswers);
}
回答by Izzy
Here's a simple rule of thumb* when it comes to webpages:
以下是有关网页的简单经验法则*:
html - content
html - 内容
css - styling
css - 样式
javascript - behaviour
javascript - 行为
Just wack some really simple JS in there instead of just $totalcorrectand it should update in real-time.
只需在其中添加一些非常简单的 JS,而不仅仅是$totalcorrect它应该实时更新。
PHP is server sided and only runs once on the server (and outputs your webpage to the client). JS is client sided and will run for as long as you tell it to* on the client.
PHP 是服务器端的,只在服务器上运行一次(并将您的网页输出到客户端)。JS 是客户端的,只要您在客户端上告诉它*,它就会运行。
(*General jist, not exactly 100% correct, but functional)
(*通用 jist,不完全 100% 正确,但功能正常)
EDIT: If you're following a PHP Tutorial this wouldn't help
编辑:如果您正在学习 PHP 教程,这将无济于事
回答by Iver Saladaga Anoos
Try this: i edited the code of Gillian lo wong. i added a score at the end and also display your wrong answer.
试试这个:我编辑了 Gillian lo wong 的代码。我在最后加了一个分数,还显示了你的错误答案。
<?php
$Questions = array(
1 => array(
'Question' => '1. CSS stands for',
'Answers' => array(
'A' => 'Computer Styled Sections',
'B' => 'Cascading Style Sheets',
'C' => 'Crazy Solid Shapes'
),
'CorrectAnswer' => 'B'
),
2 => array(
'Question' => '2. What is the Capital of the Philippines',
'Answers' => array(
'A' => 'Cebu City',
'B' => 'Davao City',
'C' => 'Manila City'
),
'CorrectAnswer' => 'C'
)
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
} else {
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;
}
echo '<br /><hr>';
if ($counter=="")
{
$counter='0';
$results = "Your score: $counter/2";
}
else
{
$results = "Your score: $counter/2";
}
} echo $results;
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php } ?>
<?php } ?>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>

