dtiopy6088 2018-12-31 22:32
浏览 11

如何在这个简单的测验应用中保持得分?

How would I keep score in this simple quiz. I have an array with the questions and answers. I put the answers in another array to shuffle them. Now I don't know how to access the "correctAnswer" to keep score. Because the correctAnswer is in another array when I vardump it the answers come up at answer1, answer2 and answer3 instead of correctAnswer, firstIncorrectAnswer, and secondIncorrectAnswer. Here is my array and code.

    <!DOCTYPE html>
<?php
 include ("inc/questions.php");
 session_start();

$pageTitle = "Math Quiz: Addition";

// if (isset($_SESSION["correctAnswer"]) && isset($_POST[$_SESSION["correctAnswer"]])) {
//     $_SESSION["score"] += 1;

// if (!isset($_SESSION["score"])) {
//     $_SESSION["score"] = 0;

if(!$_SESSION) {
  shuffle($questions);
  $_SESSION['questions'] = $questions;
}


if ((!isset($_SESSION["counter"]) || $_SESSION["counter"] >9)){

  $_SESSION["counter"] = 1;

} else {
    $_SESSION["counter"] += 1;
}

/*need to find something that's == $_SESSION['questions'][$index]["correctAnswer"]*/
$index = $_SESSION["counter"] - 1;

$choices = [
  $_SESSION['questions'][$index]["correctAnswer"],
  $_SESSION['questions'][$index]["firstIncorrectAnswer"],
  $_SESSION['questions'][$index]["secondIncorrectAnswer"],
];
shuffle($choices);

?>

<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 " . $_SESSION['questions'][$index]["leftAdder"] . " + " . $_SESSION['questions'][$index]["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>
            <?php
            if($_SESSION['counter'] == 10) {
              session_destroy();
            } ?>
        </div>
    </div>

</body>
</html>


<?php
$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
    ];
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制