我有一个我一直在使用的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(); ?>
答案 0 :(得分:0)
有点晚了,但这是一种方法:
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>';
}
答案 1 :(得分:0)
尝试一下:
foreach( $Questions as $QuestionNo => $Value ) {
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['Answers'][$Answers[$QuestionNo]].'</span>';
} else {
echo 'You entered correctly:<br />'.'<span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>';
}
echo '</blockquote>';
}