944,167 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 11906
  • PHP RSS
Jul 18th, 2007
0

Creating a multi-dimensional Session variable

Expand Post »
Hi again,

I'm stumped. I'd like to store results from a questionnaire in a multi-dimensional session variable to tally the number of questions answered after the entire questionnaire is completed. Users choose selections from checkboxes, so I have that info coming from an array. I was hoping I could just create an array of answers, keyed by the question, but I'm having some issues. Here is a code snippet.

PHP Syntax (Toggle Plain Text)
  1. //items is the checkbox
  2. //$curr is a string created designating the question
  3. $answers = $_POST['items'];
  4. if (count($answers) > 0)
  5. {
  6. $_SESSION['answers'][$curr]=$answers;
  7.  
  8. print_r($_SESSION['answers']);
  9. }

When I print out the contents of the array, it gives me:
Array ( [0] => 31 [1] => 32 [2-2] => Array ( [0] => 31 [1] => 32 [2-2] => Array *RECURSION* ) )

I feel like it should be:
Array( [2-2] => Array ( [0] => 31 [1] => 32 )
At least that's what I want to happen. I also only have one php page and depending on what's passed into the url, different questions are shown. Because of this, it seems like my answers array isn't adding the new arrays.

Weird thing is when I hardcoded everything, this worked just fine. Now that I want it more dynamic, it isn't working. Any ideas? Or maybe there's some other way I'm not seeing. TIA!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
concordia is offline Offline
3 posts
since Jul 2007
Jul 18th, 2007
0

Re: Creating a multi-dimensional Session variable

Try the
print_r($_SESSION['answers']);
before you set to see how it looks.

This might help with the debugging.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
SStedman is offline Offline
54 posts
since Jun 2007
Jan 19th, 2011
-1
Re: Creating a multi-dimensional Session variable
PHP Syntax (Toggle Plain Text)
  1. <?
  2. //items is the checkbox
  3. //$curr is a string created designating the question
  4. $answers = $_POST['items'];
  5. if (count($answers) > 0)
  6. {
  7. // Loop through the answers, and for each answer assign the value to $a
  8. foreach($answers as $a):
  9. // Then make an array within the answers and $curr array and store value of $a
  10. $_SESSION['answers'][$curr][]=$a;
  11. endforeach;
  12.  
  13. // Print_r to check array
  14. print_r($_SESSION['answers']);
  15. }
  16. ?>

That should work - I have not tested it, but it should loop through each value from the checkboxes and put it into the array like you want.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kroche is offline Offline
52 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: help
Next Thread in PHP Forum Timeline: insert mysql if !exists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC