如何使用PHP来计算测验的分数?

时间:2016-01-08 06:20:21

标签: php html function count

我正在进行测验。我想计算用户提交尝试后用户获得的总分数。如何创建显示用户提交答案后获得的标记的结果页面。我如何获得总分数,使得他们获得的每个正确答案为1 /(测验中的问题总数)?

这是我到目前为止所做的,而且我似乎无法让它发挥作用。

<?php
require_once 'getData.php';
require_once 'testing.php';



foreach( $quizHistoryQ as $index => $arr ){
    $answer=$arr['answer'];  


    if (!isset($_POST['submitter'])) {
    } else {
        $num = (int) $_POST['num'];
        $postedanswer = str_replace("_"," ",$_POST['answer']);
        if ($postedanswer == $answers[$num]['0']) {
            $_SESSION['score']++;
            $_SESSION['correct'][] = $postedanswer;
        } else {
            $_SESSION['wrong'][] = $postedanswer;
        }
        if ($num &lt; count($questions)-1) {
            $num++;
        } else {
            $last = true;
            $_SESSION['finished'] = 'yes';
        }
    }
}

这是我的问题脚本

<?php

$Titles = array("quizHistoryQ"=>"History Quiz", "quizMathQ" =>" Math Quiz", "quizHTMLq" => "HTML Quiz"); 

$quizHistoryQ = [
    "Q1" => ["questions"=>"ABC?",
        "options" => ["this is option 1",
        "this is option 2",
        "this is option 3"],
        "answer" =>2
    ],
    "Q2" => ["questions"=>"This is the Question String for question 2",
        "options" => ["this is option A",
        "this is option B",
        "this is option C"],
        "answer" =>1
    ],
    "Q3" =>["questions"=>"This is the Question String for question 3",
        "options" => ["this is option X",
        "this is option Y",
        "this is option Z"],
        "answer" =>0
    ]
];

echo 'Title:'. $Titles['quizHistoryQ'];

foreach( $quizHistoryQ as $index => $arr ){
    $question=$arr['questions'];
    $options=$arr['options'];
    $answer=$arr['answer']; 

    echo '<h3>Question: '.$index.': '.$question.'</h3>';
    echo '<ul>';
    foreach ($options as $i => $options) {
        echo " <br> <input type='radio' name='{$index}[]' value='{$i}'/>{$options}";
    }
    echo '</ul>';
}

我在这几行中遇到错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

看起来你没有正确比较答案。

改变这个:

if ($postedanswer == $answers[$num]['0']) {

对此:

if ($postedanswer == $answer) {

另外,您是否在代码中的任何位置拨打session_start?目前尚不清楚你是否这样做。

答案 1 :(得分:0)

首先,这是错误的:

if ($num &lt; count($questions)-1) {
HTML中使用

&lt;来编写<字符,但您无法在PHP中使用它。将其更改为:

if ($num < count($questions)-1) {

这将处理编辑器中显示的错误。

下一步:

if ($postedanswer == $answers[$num]['0']) {

这应该是

if ($postedanswer == $answer) {

因为您已在此处为当前问题分配了正确的答案:

 $answer=$arr['answer'];

我发现了另一件事:您并没有真正阅读从表单提交的价值。这一行:

$postedanswer = str_replace("_"," ",$_POST['answer']);

需要一个名称为answer的表单元素,您可能不会拥有该表单元素。您没有发布完整的表单代码,所以这是猜测,但由于您使用name='{$index}[]'定义了您的单选按钮,这似乎是一个安全的猜测。

这个名称无论如何都是不切实际的,你最终会得到每个答案只包含一个元素的数组,因为$index是问题数组的索引,Q1Q2等等。你会最终得到一个$_POST数组,如下所示:

array(3) {
  ["Q1"]=>
  array(1) {
    [0]=>
    int(1)
  }
  ["Q2"]=>
  array(1) {
    [0]=>
    int(5)
  }
  ["Q3"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

假设每个问题只有一个答案是正确的,您应该将radioboxes的名称定义更改为:

name='answers[{$index}]'

这将导致$_POST包含如下所示的单个数组:

array(1) {
  ["answers"]=>
  array(3) {
    ["Q1"]=>
    int(1)
    ["Q2"]=>
    int(5)
    ["Q3"]=>
    int(2)
  }
}

这使您可以循环问题并轻松比较它们:

foreach( $quizHistoryQ as $index => $arr ){
    $answer = $arr['answer']; 
    $postedanswer = intval( $_POST['answers'][$index] );
    if ( $answer == $postedanswer ) {
        $_SESSION['score']++;
    }
}