Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Session Variables Not Saving in an array

$
0
0

Hi everybody, I created a trivia game which lets the user answer questions. So far I have created a skeleton version of it. The problem I am having is my variables are not saving in an array - the way I would like them to. I have a session array created. Also if you are using it the questions aren't answered yet. Here is my code :

 

<html>
<head>
<title>Trivia</title>
</head>
<?php
//Hides non-harmful errors
error_reporting(E_ALL ^ E_NOTICE);
//Gets the content from the question text file
$file = $_SERVER['DOCUMENT_ROOT'] . "/class/Assignment1/questions.txt";
$contents = file($file);
//Session Start
session_start();
 
$answerOne = $_POST['answerOne'];
$answerTwo = $_POST['answerTwo'];
$answerThree = $_POST['answerThree'];
$answerFour = $_POST['answerFour'];
$answerFive = $_POST['answerFive'];
$answerSix = $_POST['answerSix'];
 
//Declaring my session variables for answers/questions
$_SESSION['answers'] = array($answerOne, $answerTwo, $answerThree, $answerFour, $answerFive, $answerSix);
$_SESSION['contents'] = array($contents[0], $contents[1], $contents[2], $contents[3], $contents[4], $contents[5]);
$answerArray = $_SESSION['answers'];
$questionsArray = $_SESSION['contents'];
//Declaring my variables
$answer = "answerOne";
$text = "text";
$submit = "submit";
$questions = $questionsArray[0];
 
//If the button is clicked.
if (isset($_POST['submit']) == true ){
        $clickCount = intval($_POST['clickCount']);
        $clickCount += 1;
        $questions  = $questionsArray[1];       
 
//If the clickCount = 1
if($clickCount == 1){
            $answer = "answerTwo";
        
//If the clickCount = 2
        }if($clickCount == 2){
            $answer = "answerThree";
            $questions = $questionsArray[2];
 
//if the clickCount = 3
        }if($clickCount == 3){
$answer = "answerFour";
$questions = $questionsArray[3];
 
//If the clickCount = 4
}if($clickCount == 4){
$answer = "answerFive";
$questions = $questionsArray[4];
 
//If the clickCount = 5
}if($clickCount == 5){
$answer = "answerSix";
$questions = $questionsArray[5];
 
//If the clickCount = 6
}if($clickCount == 6){
$text = "hidden";
$submit = "hidden";
$questions = "";
            print_r($answerArray) . "<br />";
           }
}
 
 
 
?>
<body>
<form action="trivia1.php" method="post">
<input type="hidden" name="clickCount" value="<?php echo $clickCount; ?>">
<label><?php echo $questions; ?></label>
<input type="<?php echo $text; ?>" name="<?php echo $answer; ?>">
<input type="<?php echo $submit; ?>" name="submit">
</form>
</body>
 
</html>

Viewing all articles
Browse latest Browse all 13200

Trending Articles