Jump to content


Photo

Session Variables Not Saving in an array

session array saving variables php

  • Please log in to reply
8 replies to this topic

#1 Tyler7028

Tyler7028
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 September 2013 - 02:45 PM

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>


#2 vinny42

vinny42
  • Members
  • PipPipPip
  • Advanced Member
  • 414 posts

Posted 18 September 2013 - 02:54 PM

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.


Edited by vinny42, 18 September 2013 - 02:56 PM.


#3 Tyler7028

Tyler7028
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 September 2013 - 04:39 PM

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.



#4 vinny42

vinny42
  • Members
  • PipPipPip
  • Advanced Member
  • 414 posts

Posted 18 September 2013 - 04:45 PM

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.



#5 Tyler7028

Tyler7028
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 September 2013 - 04:47 PM

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?



#6 vinny42

vinny42
  • Members
  • PipPipPip
  • Advanced Member
  • 414 posts

Posted 18 September 2013 - 05:15 PM

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 :-)



#7 Tyler7028

Tyler7028
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 September 2013 - 07:39 PM

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.



#8 Tyler7028

Tyler7028
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 September 2013 - 07:42 PM

It is as if the variable just doesn't go anywhere after.



#9 vinny42

vinny42
  • Members
  • PipPipPip
  • Advanced Member
  • 414 posts

Posted 19 September 2013 - 08:36 AM

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!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users