Tyler7028 Posted September 18, 2013 Share Posted September 18, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/ Share on other sites More sharing options...
vinny42 Posted September 18, 2013 Share Posted September 18, 2013 (edited) 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 September 18, 2013 by vinny42 Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450061 Share on other sites More sharing options...
Tyler7028 Posted September 18, 2013 Author Share Posted September 18, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450084 Share on other sites More sharing options...
vinny42 Posted September 18, 2013 Share Posted September 18, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450087 Share on other sites More sharing options...
Tyler7028 Posted September 18, 2013 Author Share Posted September 18, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450088 Share on other sites More sharing options...
vinny42 Posted September 18, 2013 Share Posted September 18, 2013 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450091 Share on other sites More sharing options...
Tyler7028 Posted September 18, 2013 Author Share Posted September 18, 2013 I did this. and this is what happens. array (size=2)'answers' =>array (size=6)0 => null1 => null2 => null3 => null4 => null5 => string 'g' (length=1) I entered at least a letter/number in each of them. Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450143 Share on other sites More sharing options...
Tyler7028 Posted September 18, 2013 Author Share Posted September 18, 2013 It is as if the variable just doesn't go anywhere after. Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450145 Share on other sites More sharing options...
vinny42 Posted September 19, 2013 Share Posted September 19, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/282248-session-variables-not-saving-in-an-array/#findComment-1450216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.