Jak mogę policzyć punkty w tym quizie? Array pokazuje poprawne odpowiedzi, ale jak mogę uzyskać dostęp do niego później po złożeniu formularza?

<?php $Questions = array(
1 => array(
    'Question' => 'What is 2+2:',
    'Answers' => array(
        'A' => '4',
        'B' => '5',
        'C' => 'Nothing'
    ),
    'CorrectAnswer' => 'A'
),
2 => array(
    'Question' => 'Where is Paris?',
    'Answers' => array(
        'A' => 'Somewhere',
        'B' => 'in Rome',
        'C' => 'in France'
    ),
    'CorrectAnswer' => 'C'
),
 3 => array(
    'Question' => 'What is water?',
    'Answers' => array(
        'A)' => 'hydrogen and nitrogen',
        'B)' => 'hydrogen and oxygen',
        'C)' => 'hydrocarbon and iron'
    ),
    'CorrectAnswer' => 'B'
)); ?>

I to jest kod do wydrukowania formularza: celowo chciałbym również "echo" poprawne odpowiedzi w zielonych i niewłaściwych odpowiedzi na czerwono na następnej stronie.

<form action="" method="post" name="quizz" 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>

Więc brakuje kodu, którego należy użyć po trafieniu przycisku przesyłania. Wiem, jak robić quiz bez tablic, ale ten jest dla mnie wyzwanie. Jestem nauczycielem, po prostu chcę poprawić moje zajęcia dla moich uczniów. Każda pomoc uprzejmie doceniła :)

php
1
Michael 13 marzec 2021, 13:55

1 odpowiedź

Najlepsza odpowiedź

Opublikować na tej samej stronie i zaznacz obok pytań:

<?php 

    $questions = array(
        1=>array(
            'question' => 'What is 2+2?',
            'answers' => array(
                'a' => '4',
                'b' => '5',
                'c' => 'nothing'
            ),
            'correct' => 'a'
        ),
        2=>array(
            'question' => 'Where is paris?',
            'answers' => array(
                'a' => 'somewhere',
                'b' => 'in rome',
                'c' => 'in france'
            ),
            'correct' => 'c'
        ),
        3=>array(
            'question' => 'What is water?',
            'answers' => array(
                'a' => 'hydrogen and nitrogen',
                'b' => 'hydrogen and oxygen',
                'c' => 'hydrocarbon and iron'
            ),
            'correct' => 'b'
        )
    );
?>

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        
        
        $score=0;
        $answers=array();
        
        foreach( $_POST['answers'] as $index => $answer ){
            if( array_key_exists( (int)$index, $questions ) ){
                if( $questions[ (int)$index ]['correct']==$answer ) {
                    $score++;
                }
                $answers[(int)$index]=array(
                    'question'  =>  $questions[ (int)$index ]['question'],
                    'answer'    =>  $answer,
                    'correct'   =>  $questions[ (int)$index ]['correct']==$answer
                );
            }
            
        }
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form method="post" name="quizz" id="quiz">
            <?php
                if( $_SERVER['REQUEST_METHOD']=='POST' ){
                    printf('You scored %s/%s',$score,count($questions));
                }
            ?>
            <ol>
            <?php foreach( $questions as $questionno => $value ){ ?>

            <li>
                <h4><?php 
                    echo $value['question'];
                    if( $_SERVER['REQUEST_METHOD']=='POST' ){
                        echo $answers[$questionno]['correct'] ? '<span style="color:green">Correct</span>' : '<span style="color:red">Incorrect</span>';
                        
                    }
                ?></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>

    </body>
</html>

Aby opublikować quiz do a-n-other strona, musisz znać pytania i odpowiedzi na tej innej stronie. W tym celu najłatwiejszą metodą osiągnięcia może być użycie dołączonego pliku zarówno dla quizów, jak i wyników stron.

Dołączony plik z pytaniami (pytania.php)

<?php
    #questions.php

    $questions = array(
        1=>array(
            'question' => 'What is 2+2?',
            'answers' => array(
                'a' => '4',
                'b' => '5',
                'c' => 'nothing'
            ),
            'correct' => 'a'
        ),
        2=>array(
            'question' => 'Where is paris?',
            'answers' => array(
                'a' => 'somewhere',
                'b' => 'in rome',
                'c' => 'in france'
            ),
            'correct' => 'c'
        ),
        3=>array(
            'question' => 'What is water?',
            'answers' => array(
                'a' => 'hydrogen and nitrogen',
                'b' => 'hydrogen and oxygen',
                'c' => 'hydrocarbon and iron'
            ),
            'correct' => 'b'
        )
    );
?>

Strona quizowa, która wskazuje formularz do strony wyników, a nie sam:

<?php
    require __DIR__ . '/questions.php';
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form action='quizresults.php' method="post">

            <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>

    </body>
</html>

A strona, która jest ukierunkowana przez formularz, quizresults.php

<?php
    #quiz results
    require __DIR__ . '/questions.php';
?>
<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        
        $score=0;
        
        foreach( $_POST['answers'] as $index => $answer ){
            if( array_key_exists( (int)$index, $questions ) ){
                
                if( $questions[ (int)$index ]['correct']==$answer ) {
                    $score++;
                }
                $answers[(int)$index]=(object)array(
                    'question'  =>  $questions[ (int)$index ]['question'],
                    'answer'    =>  $answer,
                    'correct'   =>  $questions[ (int)$index ]['correct']==$answer
                );
            }
        }
        
        foreach($answers as $index => $arr){
            printf(
                '<div>[ Question %d ] %s Answer: %s %s</div>', 
                $index, 
                $arr->question, 
                $arr->answer, $arr->correct ? '<span style="color:green">Correct</span>' : '<span style="color:red">Incorrect</span>'
            );
        }
    }
?>

Aktualizacja : zgodnie z komentarzem, że rzeczywista odpowiedź powinna być kolorowa zielona / czerwona

<?php
    #quiz results
    require __DIR__ . '/questions.php';
?>

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        
        $score=0;
        
        foreach( $_POST['answers'] as $index => $answer ){
            if( array_key_exists( (int)$index, $questions ) ){
                
                if( $questions[ (int)$index ]['correct']==$answer ) {
                    $score++;
                }
                $answers[(int)$index]=(object)array(
                    'question'  =>  $questions[ (int)$index ]['question'],
                    'answer'    =>  $answer,
                    'correct'   =>  $questions[ (int)$index ]['correct']==$answer
                );
            }
        }
        
        
        
        echo '<ol>';
        
        $percentage=round( ( $score / count( $questions ) ) * 100, 1 );
        
        $message='Appalling!';
        if( $percentage > 25 )$message='Bad luck!';
        if( $percentage > 50 )$message='Well done!';
        if( $percentage > 75 )$message='Congratulations!';
        if( $percentage == 100 )$message='Outstanding!';
        
        
        printf('<h2>%s You scored: %d/%d ( %s%% )</h2>', $message, $score, count( $questions ), $percentage );
        
        foreach( $questions as $questionno => $value ){
            
            $options=array();
            
            foreach( $value['answers'] as $letter => $answer ){
                $label = 'question-'.$questionno.'-answers-'.$letter;
                $checked='';
                
                if( isset( $answers[ $questionno ] ) && trim( $answers[ $questionno ]->answer )==trim( $letter ) ){
                    switch( $answers[ $questionno ]->correct ){
                        case true:
                            $colour='green';
                            $adjective=' - Correct';                        
                        break;
                        case false:
                            $colour='red';
                            $adjective=' - Incorrect';                      
                        break;
                    }
                    $answer=sprintf('<span style="color:%s">%s%s</span>', $colour, $answer,$adjective );
                    $checked=' checked';
                }
                
                
                
                $options[]=sprintf(
                    '<div>
                        <input type="radio" name="answers[%1$d]" id="%2$s" value="%3$s" %5$s/>
                        <label for="%2$s">%4$s</label>
                    </div>',
                    $questionno,
                    $label,
                    $letter,
                    $answer,
                    $checked
                );
            }
            
            
            printf(
                '<li>
                    <h4>%s</h4>
                    %s
                </li>',
                $value['question'],
                implode(PHP_EOL,$options)
            );
        }
        echo '</ol><a href="javascript:history.go(-1)">OK - Go back</a>';
    }
?>
1
Professor Abronsius 14 marzec 2021, 07:35