【问题标题】:I am making a PHP quiz app. How do I make sure the questions are asked only once?我正在制作一个 PHP 测验应用程序。我如何确保这些问题只被问到一次?
【发布时间】:2018-12-21 21:23:20
【问题描述】:

我有十个问题和答案。我使用arry_randshuffle 来随机化问题并随机排列答案。

我如何确保这些问题只被问到一次?在我遍历所有十个问题后,我希望游戏结束。谢谢你的帮助!:)

这是我目前所拥有的。

include ("inc/questions.php");
session_start();

$pageTitle = "Math Quiz: Addition";

$_SESSION["score"]= 0;
if ((isset($_SESSION["score"]) && $choices[0]["correctAnswer"])){
    $_SESSION["score"] += 1;
}
echo $_SESSION["score"];

$rand = array_rand($questions,1);
shuffle($questions);

$choices = [
    $questions[0]["correctAnswer"],
    $questions[0]["firstIncorrectAnswer"],
    $questions[0]["secondIncorrectAnswer"],
];
shuffle($choices);

if ((!isset($_SESSION["counter"]) || $_SESSION["counter"] >9)) {
    $_SESSION["counter"] = 1;
} else {
    $_SESSION["counter"] += 1;
}
?>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title><?php echo "$pageTitle";?> </title>
        <link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,400italic,700,700italic' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/styles.css">
    </head>
    <body>
        <div class="container">
            <div id="quiz-box">

                <p class="breadcrumbs"> <?php echo "Question Number "  . $_SESSION["counter"] . " of " . "10"; ?>
                <p class="quiz"><?php echo "What is " . $questions[0]["leftAdder"] . " + " . $questions[0]["rightAdder"];?></p>
                <form action="index.php" method="post">
                    <input type="hidden" name="id" value="0"/>
                    <input type="submit" class="btn" name="answer1" value="<?php echo $choices[0];?>" />
                    <input type="submit" class="btn" name="answer2" value="<?php echo $choices[1];?>" />
                    <input type="submit" class="btn" name="answer3" value="<?php echo $choices[2];?>" />
                </form>

            </div>
        </div>
    </body>
</html>

这里有一系列问题和答案:

$questions[] =
[
    "leftAdder" => 3,
    "rightAdder" => 4,
    "correctAnswer" => 7,
    "firstIncorrectAnswer" => 8,
    "secondIncorrectAnswer" => 10,
];
$questions[] =
[
    "leftAdder" => 16,
    "rightAdder" => 32,
    "correctAnswer" => 48,
    "firstIncorrectAnswer" => 52,
    "secondIncorrectAnswer" => 61,
];
$questions[] =
[
    "leftAdder" => 45,
    "rightAdder" => 12,
    "correctAnswer" => 57,
    "firstIncorrectAnswer" => 63,
    "secondIncorrectAnswer" => 55,
];
$questions[] =
[
    "leftAdder" => 42,
    "rightAdder" => 18,
    "correctAnswer" => 60,
    "firstIncorrectAnswer" => 69,
    "secondIncorrectAnswer" => 57
];
$questions[] =
[
    "leftAdder" => 96,
    "rightAdder" => 20,
    "correctAnswer" => 116,
    "firstIncorrectAnswer" => 120,
    "secondIncorrectAnswer" => 110
];
$questions[] =
[
    "leftAdder" => 44,
    "rightAdder" => 85,
    "correctAnswer" => 129,
    "firstIncorrectAnswer" => 132,
    "secondIncorrectAnswer" => 126
];
$questions[] =
[
    "leftAdder" => 51,
    "rightAdder" => 35,
    "correctAnswer" => 86,
    "firstIncorrectAnswer" => 96,
    "secondIncorrectAnswer" => 82
];
$questions[] =
[
    "leftAdder" => 5,
    "rightAdder" => 61,
    "correctAnswer" => 66,
    "firstIncorrectAnswer" => 65,
    "secondIncorrectAnswer" => 74
];
$questions[] =
[
    "leftAdder" => 26,
    "rightAdder" => 19,
    "correctAnswer" => 45,
    "firstIncorrectAnswer" => 40,
    "secondIncorrectAnswer" => 39
];
$questions[] =
[
    "leftAdder" => 26,
    "rightAdder" => 35,
    "correctAnswer" => 61,
    "firstIncorrectAnswer" => 59,
    "secondIncorrectAnswer" => 51
];

【问题讨论】:

    标签: php arrays forms session


    【解决方案1】:

    您可以通过在每个会话中仅将问题随机播放一次并将该随机后的版本保存在会话变量中来实现这一点,每次都会弹出一个问题。

    其他一些问题:

    • 每次将分数重置为零。
    • 要检查用户提交的答案是否正确,您需要查看$_POST[] 数组

    您可以使用以下代码:

    // Check submitted answer is correct:
    if (isset($_SESSION["correctAnswer"]) && isset($_POST[$_SESSION["correctAnswer"]])) {
        $_SESSION["score"] += 1;
    }
    
    if (!isset($_SESSION["score"]) {
        $_SESSION["score"] = 0;
        $_SESSION["counter"] = 0;
        shuffle($questions);
        $_SESSION["questions"] = $questions;
    }
    
    echo "score = " . $_SESSION["score"];
    
    if (count($_SESSION["questions"]) == 0) {
        // Here you should navigate to a game-over page that 
        // displays the score and allows to start again (clearing the session).
        die ("No more questions");
    }
    
    // Get next question
    $currQuestion = array_pop($_SESSION["questions"]);
    $_SESSION["correctAnswer"] = $currQuestion["correctAnswer"];
    
    $choices = [
        $currQuestion["correctAnswer"],
        $currQuestion["firstIncorrectAnswer"],
        $currQuestion["secondIncorrectAnswer"],
    ];
    shuffle($choices);
    
    $_SESSION["counter"] += 1;
    

    在其余代码中,将出现的 $questions[0] 替换为 $currQuestion

    【讨论】:

    • 非常感谢!!我现在正在工作,但我一回到家就会尝试!
    • 我会在工作时更深入地研究 $_POST 数组!
    【解决方案2】:

    代替:

    $rand = array_rand($questions,1);
    shuffle($questions);
    

    <div class="container">
            <div id="quiz-box">
    <?php
    
        shuffle($questions); 
        for($i=0; $i<count($questions); $i++) {
    ?>
                <p class="breadcrumbs"> <?php echo "Question Number "  . $i . " of " . "10"; ?>
                <p class="quiz"><?php echo "What is " . $questions[$i]["leftAdder"] . " + " . $questions[$i]["rightAdder"];?></p>
                <form action="index.php" method="post">
                    <input type="hidden" name="id" value="0"/>
                    <input type="submit" class="btn" name="answer1" value="<?php echo $choices[$i][0];?>" />
                    <input type="submit" class="btn" name="answer2" value="<?php echo $choices[$i][1];?>" />
                    <input type="submit" class="btn" name="answer3" value="<?php echo $choices[$i][2];?>" />
                </form>
    
            </div>
        </div>
    <?php
    
        }
    ?>
    

    【讨论】:

    • 感谢您的帮助。这几天我一直在挣扎。我现在正在工作,但我回家后会试一试。
    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多