【问题标题】:PHP quiz - display correct and incorrect answersPHP 测验 - 显示正确和错误的答案
【发布时间】: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. '.&nbsp;&nbsp;' .$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(); ?>

【问题讨论】:

标签: php wordpress forms


【解决方案1】:

有点晚了,但这是一种方法:

    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>';
}

(取自:PHP quiz with on screen results

【讨论】:

    【解决方案2】:

    试试这个:

    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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 2019-12-07
      • 2017-05-21
      相关资源
      最近更新 更多