簡體   English   中英

在PHP中測驗三個或兩個答案

[英]Quiz with three or two answers in php

我想創建一個測驗,其中一些問題有兩個或三個正確答案。

我使用checkboxforeach但是我的代碼沒有達到我的目標。

例如,我回答5個問題中的2個,正確的給定計數器必須為2,但它給我零。

這是我的HTML部分:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form action="res.php" method="get">

    <p>question</p>
    <input type="checkbox" name="q1[]" value="1">first<br>
    <input type="checkbox" name="q1[]" value="2">second<br>
    <input type="checkbox" name="q1[]" value="3">third<br>
    <input type="checkbox" name="q1[]" value="4">fourth<br>



    <p>question</p>
    <input type="checkbox" name="q2[]" value="1">first<br>
    <input type="checkbox" name="q2[]" value="2">second<br>
    <input type="checkbox" name="q2[]" value="3">third<br>
    <input type="checkbox" name="q2[]" value="4">fourth<br>



    <p>question</p>
    <input type="checkbox" name="q3[]" value="1">first<br>
    <input type="checkbox" name="q3[]" value="2">second<br>
    <input type="checkbox" name="q3[]" value="3">third<br>
    <input type="checkbox" name="q3[]" value="4">fourth<br>



    <p>question</p>
    <input type="checkbox" name="q4[]" value="1">first<br>
    <input type="checkbox" name="q4[]" value="2">second<br>
    <input type="checkbox" name="q4[]" value="3">third<br>
    <input type="checkbox" name="q4[]" value="4">fourth<br>



    <p>question</p>
    <input type="checkbox" name="q5[]" value="1">first<br>
    <input type="checkbox" name="q5[]" value="2">second<br>
    <input type="checkbox" name="q5[]" value="3">third<br>
    <input type="checkbox" name="q5[]" value="4">fourth<br>


    <br>

    <input type="submit" name="submit" value="send">

    </body>
    </html>

這是我的PHP部分:

<?php

$q[0] = $_GET['q1'];
$q[1] = $_GET['q2'];
$q[2] = $_GET['q3'];
$q[3] = $_GET['q4'];
$q[4] = $_GET['q5'];

if (isset($_GET['submit']))
{
    $counter = 0;
    foreach($q[0] as $item)
    {
        if (in_array(1, $item) && in_array(2, $item))
        {
            $counter++;
        }
    }
    foreach($q[1] as $item)
    {
        if (in_array(1, $item) && in_array(4, $item))
        {
            $counter++;
        }
    }
    foreach($q[2] as $item)
    {
        if (in_array(2, $item) && in_array(3, $item))
        {
            $counter++;
        }
    }
    foreach($q[3] as $item)
    {
        if (in_array(4, $item))
        {
            $counter++;
        }
    }
    foreach($q[4] as $item)
    {
        if (in_array(1, $item) && in_array(2, $item) && in_array(4, $item))
        {
            $counter++;
        }
    }
}


echo $counter;
?>

我所做的第一個更改是在HTML文檔中。 在這里,我為每個復選框保留了不同的值,但是在每個組中使用了相同的一組值,因為在不同問題中對選項號使用相同的一組值不會造成損害,並且使驗證它們變得容易。 以下是HTML

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form action="res.php" method="get">

<p>question</p>
<input type="checkbox" name="q1[]" value="1">first<br>
<input type="checkbox" name="q1[]" value="2">second<br>
<input type="checkbox" name="q1[]" value="3">third<br>
<input type="checkbox" name="q1[]" value="4">fourth<br>



<p>question</p>
<input type="checkbox" name="q2[]" value="1">first<br>
<input type="checkbox" name="q2[]" value="2">second<br>
<input type="checkbox" name="q2[]" value="3">third<br>
<input type="checkbox" name="q2[]" value="4">fourth<br>



<p>question</p>
<input type="checkbox" name="q3[]" value="1">first<br>
<input type="checkbox" name="q3[]" value="2">second<br>
<input type="checkbox" name="q3[]" value="3">third<br>
<input type="checkbox" name="q3[]" value="4">fourth<br>



<p>question</p>
<input type="checkbox" name="q4[]" value="1">first<br>
<input type="checkbox" name="q4[]" value="2">second<br>
<input type="checkbox" name="q4[]" value="3">third<br>
<input type="checkbox" name="q4[]" value="4">fourth<br>



<p>question</p>
<input type="checkbox" name="q5[]" value="1">first<br>
<input type="checkbox" name="q5[]" value="2">second<br>
<input type="checkbox" name="q5[]" value="3">third<br>
<input type="checkbox" name="q5[]" value="4">fourth<br>


<br>

<input type="submit" name="submit" value="send">

</body>
</html>

添加了一個功能來檢查值,用戶選擇了您指定的內容。

<?php
if (isset($_GET['q1']))
$q[] = $_GET['q1'];
if (isset($_GET['q2']))
$q[] = $_GET['q2'];
if (isset($_GET['q3']))
$q[] = $_GET['q3'];
if (isset($_GET['q4']))
$q[] = $_GET['q4'];
if (isset($_GET['q5']))
$q[] = $_GET['q5'];

// each questions correct answers list
$ans[] = array(1,2); // for q1
$ans[] = array(1,4);   // for q2 and so on...
$ans[] = array(2,3);
$ans[] = array(4);
$ans[] = array(1,2,4);

if (isset($_GET['submit']))
{
    $counter = 0;
    for($i = 0; $i < count($q); $i++)
    {
        if (check_answer($ans[$i], $q[$i]))
        {
            $counter++;
        }
    }
}

function check_answer($answer, $selected)
{
    $valid = true;
    foreach($answer as $a)
    {
        if (!in_array($a, $selected))
        {
            $valid = false;
            break;
        }
    }

return $valid;
}

echo "Count: $counter";
?>

注意:您也應該進行錯誤檢查,以處理不良情況。

您也可以使用字符串:

$answers[0] = $_POST['q1'];
$answers[1] = $_POST['q2'];
$answers[2] = $_POST['q3'];
$answers[3] = $_POST['q4'];
$answers[4] = $_POST['q5'];

$counter = 0;
$good_answers = ['12', '14', '23', '4', '124'];

for($i = 0; $i < count($answers); $i++){
    if(implode('', $answers[$i]) == $good_answers[$i]) $counter++;
}

echo $counter;

希望能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM