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

我有一个简单的测验,我在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'];


弑天下
浏览 188回答 2
2回答

开心每一天1111

需要所有正确的答案首先,您需要选中choice输入复选框而不是单选按钮。单选按钮只能选择一次,而您可以有多个复选框。您还需要能够一次提交多个具有相同名称的文件,因此它必须是一组复选框。所以选择器应该是<input name="choice[]" type="checkbox" value="<?php echo $row['id']; ?>" />代替<input name="choice" type="radio" value="<?php echo $row['id']; ?>" />然后,当您收到它时,$_POST['choice']在PHP中是一个数组而不是单个值。您可以执行COUNT()查询以查看有多少正确答案,然后通过检查返回的行数(由计数的数量)来查看是否全部获取了正确答案-如果它们相等,它们就可以了!我还将为此使用准备好的语句(您应该在所有查询中使用该语句)。我们可以使用创建动态数量的占位符rtrim(str_repeat("?,", count($a)), ",");,并使用splat-operator传递值...。//Get correct choice$q = "SELECT id, COUNT(id) as cnt&nbsp; &nbsp; &nbsp; FROM choices c&nbsp; &nbsp; &nbsp; WHERE question_number = ?&nbsp; &nbsp; &nbsp; &nbsp; AND is_correct = 1&nbsp; &nbsp; &nbsp; &nbsp; AND id IN (".rtrim(str_repeat("?,", count($_POST['choice'])), ",").")&nbsp; &nbsp; &nbsp; GROUP BY question_number";$stmt = $mysqli->prepare($q);if ($stmt) {&nbsp; &nbsp; $stmt->bind_param("s".str_repeat("s", count($_POST['choice']), $number, ...$_POST['choice']);&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $stmt->bind_result($id, $count);&nbsp; &nbsp; $result = $stmt->fetch();&nbsp; &nbsp; if ($stmt->num_rows == $count) {&nbsp; &nbsp; &nbsp; &nbsp; echo "You got all the right answers!";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "At least one answer was incorrect.";&nbsp; &nbsp; }} else {&nbsp; &nbsp; echo "Error performing query";&nbsp; &nbsp; trigger_error($stmt->error);&nbsp; &nbsp; trigger_error($mysqli->error);}

繁星coding

SELECT * FROM choices WHERE question_number = $number AND is_correct = 1该行告诉我们,在您的选择表中,它们是一列is_correct,如果设置为1,则它将选择标记为有效答案。我不明白,为什么您不能有多个选择,将其设置为值1。由于您将有1个以上的答案,因此您可以有1个或更多选择。因此,返回的结果将是一个数组。$boolAnsCorrect = false;if ($row) {&nbsp; &nbsp; while($srow = mysqli_fetch_array($row)) {&nbsp; &nbsp; &nbsp; &nbsp; $correct_choice = $srow['id'];&nbsp; &nbsp; &nbsp; &nbsp; if($correct_choice == $selected_choice) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $boolAnsCorrect = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if ($boolAnsCorrect) {&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['score'] = $_SESSION['score'] + 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['score1'] = $_SESSION['score1'] - 1;&nbsp; &nbsp; }}else {&nbsp; &nbsp; // handle error, assuming that a quiz, MUST have ATLEAST 1 answer.}
打开App,查看更多内容
随时随地看视频慕课网APP