测验,每个问题有多个正确答案

时间:2019-05-05 18:20:34

标签: php

我有一个简单的测验,我在GitHub上进行了搜索,以亲自了解测验的工作原理。由于此测验仅针对每个问题一个正确的答案,因此我尝试查看是否可以使每个问题具有多个正确的答案,但我没有运气。

我搜索了google,发现的搜索结果完全没有帮助。

这是包含答案的问题的代码

//Get total number of questions
$query = "SELECT * FROM questions";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;

// Get Question
$number = (int) $_GET['n'];
$query = "SELECT * FROM questions WHERE question_number = $number";

//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$question = $result->fetch_assoc();


// Get Choices
$query = "SELECT * FROM choices WHERE question_number = $number";

//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);

<div class="container">
    <div class="current">Question <?php echo $number; ?> of <?php echo $total; ?> and correct <?=$_SESSION['score']?></div>
    <p class="question">
    <?php echo $question['question']?>
    </p>
    <form method="post" action="process.php">
        <ul class="choices">
            <?php foreach($choices as $row){ ?>
            <li>
                <input name="choice" type="radio" value="<?php echo $row['id']; ?>" />
                <?php echo $row['text']; ?>
            </li>
            <?php } ?>
        </ul>
        <input type="submit" value="submit" />
        <input type="hidden" name="number" value="<?php echo $number; ?>" />
    </form>
</div>

这是验证答案的代码

if($_POST){
    $number = $_POST['number'];
    $selected_choice = $_POST['choice'];
    $next = $number + 1;
    $total = 4;

    //Get total number of questions
    $query = "SELECT * FROM questions";
    $results = $mysqli->query($query) or die($mysqli->error.__LINE__);
    $total = $results->num_rows;

    //Get correct choice
    $q = "SELECT * FROM choices WHERE question_number = $number AND is_correct = 1";
    $result = $mysqli->query($q) or die($mysqli->error.__LINE__);
    $row = $result->fetch_assoc();
    $correct_choice = $row['id'];

    //compare answer with result
    if($correct_choice == $selected_choice) {
        //$_SESSION['score']++;
        $_SESSION['score'] = $_SESSION['score'] + 1;
    }
    if($correct_choice !== $selected_choice) {
        //$_SESSION['score']++;
        $_SESSION['score1'] = $_SESSION['score1'] - 1;
    }

    if($number == $total) {
        header("Location: final.php");
        exit();
    } else {
        header("Location: question.php?n=".$next);
    }
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

需要所有正确的答案

首先,您需要输入choice输入复选框,而不是 radio 按钮。单选按钮只能选择一次,而您可以有多个复选框。您还需要一次提交多个具有相同名称的文件,因此它必须是一组复选框。因此选择器应为

<input name="choice[]" type="checkbox" value="<?php echo $row['id']; ?>" />

代替

<input name="choice" type="radio" value="<?php echo $row['id']; ?>" />

然后,当您收到它时,PHP中的$_POST['choice']是一个数组而不是单个值。

您可以执行COUNT()查询以查看有多少正确答案,然后通过检查返回的行数(以计数的数量)来查看是否已全部获取正确答案-如果它们相等,则得到他们没事!

我还将为此使用准备好的语句(您应该将其用于 所有查询 )。

我们可以使用rtrim(str_repeat("?,", count($a)), ",");动态创建数量的占位符,并使用splat-operator ...传递值。

//Get correct choice
$q = "SELECT id, COUNT(id) as cnt
      FROM choices c
      WHERE question_number = ?
        AND is_correct = 1
        AND id IN (".rtrim(str_repeat("?,", count($_POST['choice'])), ",").")
      GROUP BY question_number";
$stmt = $mysqli->prepare($q);
if ($stmt) {
    $stmt->bind_param("s".str_repeat("s", count($_POST['choice']), $number, ...$_POST['choice']);
    $stmt->execute();
    $stmt->bind_result($id, $count);
    $result = $stmt->fetch();
    if ($stmt->num_rows == $count) {
        echo "You got all the right answers!";
    } else {
        echo "At least one answer was incorrect.";
    }
} else {
    echo "Error performing query";
    trigger_error($stmt->error);
    trigger_error($mysqli->error);

}

答案 1 :(得分:0)

SELECT * FROM choices WHERE question_number = $number AND is_correct = 1

该行告诉我们,在您的选择表中,它们是一列,is_correct如果设置为1,则它将选择标记为有效答案。

我不明白,为什么您不能有多个选择,将值设置为1。

由于您将拥有多个答案,因此您可以有1个或更多选择。因此,返回的结果将是一个数组。

$boolAnsCorrect = false;
if ($row) {
    while($srow = mysqli_fetch_array($row)) {
        $correct_choice = $srow['id'];
        if($correct_choice == $selected_choice) {
            $boolAnsCorrect = true;
            break;
        }
    }

    if ($boolAnsCorrect) {
        $_SESSION['score'] = $_SESSION['score'] + 1;

    } else {
        $_SESSION['score1'] = $_SESSION['score1'] - 1;
    }
}
else {
    // handle error, assuming that a quiz, MUST have ATLEAST 1 answer.
}