Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do you store multiple array values in a php $_SESSION and then display them?

    For example take the values in this following example array, (which are pulled from a MYSQL TABLE):

    User ID First Name Last Name
    01 Bob Newburg
    02 Linda Kraft
    03 James Rafferty


    Now let's say you would like to create a condition, after pulling out these values from your mysql table, that would store all the values in these three columns in the $_SESSION superglobal. Then let's say you would like to transfer all the said values to another PHP page and display them all. How would you accomplish something like this?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can assign the entire array to a $_SESSION variable which will then also be the entire array.

    PHP Code:
    $queryresults some big array;
    $_SESSION['tabledata'] = $queryresults
    To display, loop through the array using foreach().

    PHP Code:
    foreach($_SESSION['tabledata'] as $data)
    {
        echo 
    "{$data['userId']} {$data['firstname']} {$data['lastname']}"


  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm, i could swear i answered this same question by the same person just a few hours ago...

  • #4
    New Coder
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    You can assign the entire array to a $_SESSION variable which will then also be the entire array.

    PHP Code:
    $queryresults some big array;
    $_SESSION['tabledata'] = $queryresults
    To display, loop through the array using foreach().

    PHP Code:
    foreach($_SESSION['tabledata'] as $data)
    {
        echo 
    "{$data['userId']} {$data['firstname']} {$data['lastname']}"

    I tried using this solution for my php file. It is suppose to retrieve multiple lines of information in an array, as I mentioned above, and display them. When I run this program, it displays only one line of information from my array, and there should be more displayed. I don't know why it won't display them all, and if you could help me figure that out, I would greatly appreciate it.

  • #5
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Make sure that your array is in the proper format and has multiple rows returning from the query.
    PHP Code:
    $results = array (
      array (
        
    'id'=>1,
        
    'first_name'=>'Bob',
        
    'last_name'=>'Newburg'
      
    ),
      array (
        
    'id'=>2,
        
    'first_name'=>'Linda',
        
    'last_name'=>'Kraft'
      
    ),
      array (
        
    'id'=>3,
        
    'first_name'=>'James',
        
    'last_name'=>'Rafferty'
      
    )
    ); 
    Other than that I cannot be much more help because the example is how you would do it. So just do basic problem solving on it. 1. All results are returned from query and properly put into an array. 2. Once you have this array use print_r($array) to make sure all of the output is coming through. 3. Put it into your session variable. 4. Make sure you are starting sessions on both pages. 5. Use print_r on your session variable to make sure it came across. 6 Display by the method shown.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •