download20151010 2017-09-28 21:24
浏览 36

使用Session数组进行琐事游戏

I'm new to session arrays how to actually use sessions, but I am trying to build a small trivia game but I can't figure out how to display the next question after someone enters in an answer in the text box. I know it has something to do with isset but I can't figure out the right syntax. My code is simple but I believe there are errors with it currently.

I want to push the user's answer into the userAnswers SESSION Array but I don't think it is allowing me to.

Here is my code, but it is not doing too well right now:

<?php
session_start();
$counter = 0;
$_SESSION['questions'] = array();
$_SESSION['answers'] = array();
$_SESSION['userAnswers'] = array();
$file = "trivQuestions.txt";
$questions = file($file);

    foreach($questions as $var){
        $explode = explode("\t", $var);
        //wrong 
        array_push($_SESSION['questions'], $explode[0]);
        array_push($_SESSION['answers'], $explode[1]);
    }


if (!isset($_SESSION['userAnswers'][0])){
    $counter = 0;
    echo $_SESSION['questions'][$counter];
}


?>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form method = post action = trivia.php>
        <input type="text" name="answer">
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
<?php
    if(!isset($_POST['answer'])){
        array_push($_SESSION['userAnswers'], $_POST['answer']);
    }
?>

I'm just trying to work around it right now, but I just want the first question to done after they entered an answer.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?