PHP测验-显示正确和不正确的答案
我有一个php测试脚本,我一直在使用,并试图修改它,以显示正确和不正确的答案,我有问题试图完成这一点 因此,当用户选择错误答案时,我希望脚本显示错误答案,然后在下面显示正确答案。我已经弄清楚了如何显示正确答案的字母,但我希望显示的是实际答案,而不仅仅是字母 我是一个php初学者,因此任何帮助都将不胜感激 以下是完整的代码:PHP测验-显示正确和不正确的答案,php,wordpress,forms,Php,Wordpress,Forms,我有一个php测试脚本,我一直在使用,并试图修改它,以显示正确和不正确的答案,我有问题试图完成这一点 因此,当用户选择错误答案时,我希望脚本显示错误答案,然后在下面显示正确答案。我已经弄清楚了如何显示正确答案的字母,但我希望显示的是实际答案,而不仅仅是字母 我是一个php初学者,因此任何帮助都将不胜感激 以下是完整的代码: <?php get_header(); /* Template Name: PHP Quiz safety asmnt */ $Questions = array
<?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(); ?>
有点晚了,但有一个办法:
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>';
}
foreach($Questions as$QuestionNo=>$Value){
//附和这个问题
echo$Value['Question']。
;
如果($Answers[$QuestionNo]!=$Value['CorrectAnswer']){
回显“”。$Value['Answers'][$Answers[$QuestionNo].';//用类替换样式
}否则{
回显“”。$Value['Answers'][$Answers[$QuestionNo].';//用类替换样式
}
回声“
”;
}
(摘自:)试试这个:
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>';
}
foreach($Questions as$QuestionNo=>$Value){
回显$QuestionNo.'.$Value['Question'].';
如果($Answers[$QuestionNo]!=$Value['CorrectAnswer']){
echo“您输入错误:
”。$Value['Answers'][$Answers[$QuestionNo].';
回显“
”。“正确答案是:
”。$Value['Answers'][$Answers[$QuestionNo].”;
}否则{
echo“您输入正确:
”。$Value['Answers'][$Answers[$QuestionNo].';
}
回声';
}
看看这里,希望能对您有所帮助: