PHP测验单选和复选框计算 [英] PHP Quiz Radio and Checkbox Calculation

查看:71
本文介绍了PHP测验单选和复选框计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP设计一个简单的测验,并且想知道我是否在继续之前以正确的方式进行操作.

I'm designing a simple quiz with PHP and would like to know if i'm approaching it the correct way before proceeding.

测验将包含大约25个问题,其中包含单选按钮和复选框.想法是计算总数,并在提交测验时将其显示给用户.

The quiz will have approximately 25 questions, a mixture of radio buttons and checkboxes. The idea is to calculate the total and display this to the user when the quiz is submitted.

到目前为止,我只有四个问题.问题1-3是单选按钮,最多可以选择一个.问题4是一个复选框,最多允许两个选择,每个正确选择的价值为0.5

I have just four questions so far. Questions 1 - 3 are radio buttons, one selection max. Question 4 is a checkbox and allows two selections max, each correct selection is worth 0.5

这是我的html代码的摘要(已删除问题和答案文本).

Here's a snippet of my html code (question and answer text removed).

HTML

// q1 answer is value 2
<input type="radio" name="form[1-1]" value="1">
<input type="radio" name="form[1-1]" value="2">
<input type="radio" name="form[1-1]" value="3">

// q2 answer is value 1
<input type="radio" name="form[1-2]" value="1">
<input type="radio" name="form[1-2]" value="2">
<input type="radio" name="form[1-2]" value="3">

// q1 answer is value 2
<input type="radio" name="form[1-3]" value="1">
<input type="radio" name="form[1-3]" value="2">
<input type="radio" name="form[1-3]" value="3">

// q4 answer is value 1 or 3. 0.5 points each
<input type="checkbox" name="form[1-4][]" value="1">
<input type="checkbox" name="form[1-4][]" value="2">
<input type="checkbox" name="form[1-4][]" value="3">
<input type="checkbox" name="form[1-4][]" value="4">

下面的PHP代码有效,是正确的方法还是更有效的方法?特别是与复选框问题4有关.

The PHP code below works, is it the correct approach or is there a more efficient way? Specifically with the checkbox question 4.

PHP

$total = array();

$total = '0';

$q1 = $_POST['form']['1-1'];
$q2 = $_POST['form']['1-2'];
$q3 = $_POST['form']['1-3'];
$q4 = $_POST['form']['1-4'];

// answer with value 2 is correct
if ($q1 == '2' ) {
    $total++;
};
// answer with value 1 is correct
if ($q2 == '1' ) {
    $total++;
};
// answer with value 2 is correct
if ($q3 == '2' ) {
    $total++;
};
// answer with value 1 is correct
if ($q4[0] == '1' ) {
    $total = $total + 0.5;
};
// answer with value 3 is correct
if ($q4[1] == '3' ) {
    $total = $total + 0.5;
};

// send $total to database here

我不想使用JS/Jquery,我想使用PHP方法.

I'm not looking to use JS / Jquery, I want to use a PHP approach.

推荐答案

这是一个更具动态性的版本,它也很适合从数据库中加载.

This is a more dynamic version, which also lends itself to being loaded from a database.

解决方案数组包含问题和答案的列表,如果它是一个多答案问题,则答案是正确值的数组.

The solutions array has the list of questions and answers, if it is a multiple answer question, then the answer is an array of the correct values.

该循环遍历所有解决方案,并将答案与预期解决方案进行比较.如果答案不存在,则?? null对其进行设置,但该结果应与结果不匹配.

The loop goes through the solutions and compares the answer against the expected solution. If the answer is not present, the ?? null sets it, but it should not match the results.

$solutions = ['1-1' => 2, '1-2' => 1, '1-3' => 2, '1-4' => [1,3]];

foreach ( $solutions as $question => $solution ) {
    $userAnswer = $_POST['form'][$question] ?? null;
    if ( is_array($solution) ){
        $marksPerAnswer = 1/count($solution);
        $correct = array_intersect($solution, $userAnswer);
        $total += $marksPerAnswer * count($correct);
    }
    else    {
        $total += ($userAnswer == $solution);
    }
}

这篇关于PHP测验单选和复选框计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆