2

I have a php quiz script that I have been using and am trying to modify it to display both correct and incorrect answers and am having issues trying to accomplish this.

So when a user selects the incorrect answer, I want the script to display the incorrect answer and then the correct answer below it. I have figure out how to display the letter of the correct answer, but I want the actual answer to be displayed and not just the letter.

I am a beginner with php so any help would be appreciated.

Here is the entire code:

<?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(); ?>

2 Answers2

0

A little late but here's one way:

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

(taken from: PHP quiz with on screen results )

Z Kubota
  • 155
  • 1
  • 3
  • 11
0

Try this:

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>';
}
Webace
  • 11
  • 1