Jump to content

Archived

This topic is now archived and is closed to further replies.

Tyler7028

Session Variables Not Saving in an array

Recommended Posts

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>

Share this post


Link to post
Share on other sites

There are no such thing as non-harmfull errors, don't suppress errors fix them!

 

As for your question, which part of the script is failing, and how?

 

 

Also, HTML allows you to use an array-style name for fields, so you can just use $_POST['answers'] and get all the answers given, even with indexes that represent the question numbers or names.

Share this post


Link to post
Share on other sites

It seems when I run the program I want at the end to display all of the inputted answers, but for some reason it won't display.

Share this post


Link to post
Share on other sites

It seems that you want to answer a number of questions  on separate pages? Yet you alsways look at all the questions at once in $_POST and use that to fill the session. So if question one is not sent along when you answer question 2, then question 1 will be erased.

Share this post


Link to post
Share on other sites

Yah, each question is being submitted on a form (pages), but they don't go along, so it has to do with the way I am declaring my $answerOne ...  and so on variables?

Share this post


Link to post
Share on other sites

Do a var_dump($_POST) and a var_dump($_SESSION) on every page, so you can see what you are sending and what's in the session at every page, that will probably make it clearer than I can explain it :-)

Share this post


Link to post
Share on other sites

I did this. and this is what happens.

array (size=2)
'answers' =>
array (size=6)
0 => null
1 => null
2 => null
3 => null
4 => null
5 => string 'g' (length=1)

 

I entered at least a letter/number in each of them.

Share this post


Link to post
Share on other sites

Im guessing this is a screenshot from the last question?

 

Because your code keeps track of a clickcounter so you are adding one question at a time, but you are loading all questions from $_POST every time.

So, when you getto question six you are loading the first five questions from $_POST, but they don't exist on that page, so you are filling them with NULL.

 

The solution to this problem is simple; put a hidden field in the form and fill it with thw questionnumber. When the form is submitted, use that number as an index in the arrays, and increase it by one to display the next question form.

 

Only process the question that you have in $_POST, and *check* that the data you are trying to get from $_POST actually exists where!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.