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.