0

I made a quiz with the code I got from here : PHP quiz with on screen results here's the code:

<?php
error_reporting(0);
$Questions = array(
    1 => array(
        'Question' => '<p>1. A set of instructions directed at a computer to solve a problem is called...</p>',
        'Answers' => array(
            'A' => 'Logic',
            'B' => 'Algorithm',
            'C' => 'Program',
            'D' => 'Design'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '<p>2. The science of thinking or a way of thinking with various actions that have a specific purpose according to the applicable rules...</p>',
        'Answers' => array(
            'A' => 'Algorithm',
            'B' => 'Design',
            'C' => 'Logic',
            'D' => 'Program'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])) {
    $Answers = $_POST['answers']; // Get submitted answers.

    // Now this is fun, automated question checking! ;)

    foreach ($Questions as $QuestionNo => $Value) {
        // Echo the question
        echo $Value['Question'] . '<br />';

        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']) {
            echo 'Your Answer: <span style="color: red;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span><br>'; // Replace style with a class
            echo 'Correct Answer: <span style="color: green;">' . $Value['Answers'][$Value['CorrectAnswer']] . '</span>';
        } else {
            echo 'Your Answer: <span style="color: green;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span><br>'; // Replace style with a class
            echo 'Correct Answer: <span style="color: green;">' . $Value['Answers'][$Answers[$QuestionNo]] . '</span>';
            $counter++;
        }

        echo '<br /><hr>';
        if ($counter == "") {
            $counter = '0';
            $results = "<center><p><b>You answered $counter questions</b></p></center>";
        } else {
            $results = "<center><p><b>You answered $counter questions</b></p></center>";
        }
    }
    echo $results;
} else {
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
        <?php foreach ($Questions as $QuestionNo => $Value) { ?>

            <p><?php echo $Value['Question']; ?></p>
            <?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 } ?>

        <?php } ?>
        <input type="submit" value="Submit Quiz" />
    </form>
<?php
}
?>

The output of the code displays the correct and incorrect answers that have been answered along with the answer key. How can I change it to show only wrong answers? Thank you

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40

0 Answers0