【发布时间】:2014-06-02 08:42:48
【问题描述】:
我有一个我一直在使用的 php 测验脚本,我正在尝试修改它以显示正确和不正确的答案,但在尝试完成此操作时遇到了问题。
因此,当用户选择错误答案时,我希望脚本显示错误答案,然后在其下方显示正确答案。我已经弄清楚如何显示正确答案的字母,但我希望显示实际答案而不仅仅是字母。
我是 php 的初学者,所以任何帮助将不胜感激。
这是完整的代码:
<?php get_header();
/*
Template Name: PHP Quiz safety asmnt
*/
$Questions = array(
1 => array(
'Question' => 'The appropriate response during a health threatening emergency such as a fire or toxic spill is to:',
'Answers' => array(
'A' => 'Pull the fire alarm; evacuate immediately to the assemply point; and do not re-enter the cleanroom until instructed to do so.',
'B' => 'Notify lab; evacuate immediately to the assemply point; and do not re-enter the cleanroom until instructed to do so.',
'C' => 'Call EH&S'
),
'CorrectAnswer' => 'A'
),
2 => array(
'Question' => 'With proper use of wet bench and disposal procedures, the laboratory should be free of odors. In general, if you smell something, you should:',
'Answers' => array(
'A' => 'Call EH&S; Clear affected area if necessary; Notify staff and other users',
'B' => 'Do nothing; odd smells are normal when working in the cleanroom.',
'C' => 'Notify staff member; clear affected area; wait for instruction from staff who will investigate'
),
'CorrectAnswer' => 'C'
)
);
?>
<div id="bodyPage" class="clear">
<!------- start body ------->
<section id="contentPage">
<h1 class="title"><?php the_title(); ?></h1>
<?php
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
echo '<br />';
// Automated question checking!)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $QuestionNo. '. ' .$Value['Question'].'<blockquote>';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You entered incorrectly:<br />'.'<span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
echo '<br/>'.'The correct answer is:<br />'.'<span style="color: green;">'.$Value['CorrectAnswer'][$Answers[$QuestionNo]].'</span>';
} else {
echo 'You entered correctly:<br />'.'<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
}
echo '</blockquote>';
}
} else {
?>
<form action="<?php the_permalink(); ?>" method="post" id="quiz">
<ol>
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<li>
<h4><?php echo $Value['Question']; ?></h4>
<?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 } ?>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>
<!------- end body ------->
</div>
<?php get_footer(); ?>
【问题讨论】:
-
看看这里,希望对你有帮助:stackoverflow.com/questions/4719326/…