【问题标题】:PHP Quiz using array to generate questions. How to count points?PHP Quiz 使用数组生成问题。如何计算积分?
【发布时间】:2021-03-13 10:55:08
【问题描述】:

我如何计算这个测验的分数?数组显示正确答案,但提交表单后如何访问它?

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

这是打印表单的代码:我特意还想在下一页“回显”绿色的正确答案和红色的错误答案。

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

所以缺少的是用户点击提交按钮后我应该使用的代码。我知道如何在没有数组的情况下进行测验,但这对我来说很有挑战性。我是一名教师,只想为我的学生改进我的课程。 非常感谢每一个帮助:)

【问题讨论】:

  • 表单操作设置为同一页面,那么next page 是什么?您打算如何称呼它?
  • other page 需要知道$questions 数组的内容才能标记答案。这是复制内容还是通过includes完成?

标签: php


【解决方案1】:

要发布到同一页面并在问题旁边标记:

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

要将测验发布到a-n-other 页面,您需要知道其他页面上的问题和答案。为此,实现这一目标的最简单方法可能是为测验和结果页面使用包含的文件。

包含问题的文件( questions.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'
        )
    );
?>

将表单指向结果页面而不是其自身的测验页面:

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

以及表单所针对的页面,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>'
            );
        }
    }
?>

更新:根据评论,实际答案应为绿色/红色

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

【讨论】:

  • 非常感谢!你真是太好了!
  • 如果有帮助,也许考虑接受答案?
  • 好的,我会试试的,这是我第一次使用stackoverflow,我正在学习如何使用它。
  • 你知道如何用绿色标记正确答案,用红色标记错误答案吗?我的意思是数组的一部分(在打印之后)应该在提交按钮后改变颜色,而不是问题旁边出现的新文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
  • 2021-09-23
相关资源
最近更新 更多