tags:

views:

61

answers:

2

Hi Everyone, my session variable seems to keep dropping its array values. What could I be doing wrong? Is it my object?

session_start() is initiated at the beginning,

if(isset($_SESSION['locations'])) { 
     unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']); 
     echo "session exists"; 
}
else{
     $_SESSION['locations'] = serialize(new Location());
     unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']);
     echo "session does not exist";
}        

class Location{

    function listItems($location){

    $array;

    $array[] = $location;

    //print_r($array);

    // Re-index:

    $array = array_values($array);

    print_r($array);

    $count = count($array);

    for ($i = 0; $i < $count; $i++) {

        echo "{$array[$i]}\n";

    }

    }

}
+4  A: 
mmattax
The class is working.. up to a point I guess. I just want to accept values, and add them to the array, then print the array.
Idealflip
I re-wrote your location class based on your comment
mmattax
Wow, thanks! Pretty amazing lol.
Idealflip
+2  A: 

The first things that come to mind:

  1. Are you remembering to use session_start() at the top of every page, before headers are sent? if not, then no session information is stored between pages.
  2. $_SESSION['locations'] is being set to an object. You never serialized the object, which can cause issues trying to store it into a session. See http://www.php.net/manual/en/language.oop5.serialization.php for more information on object serialization, and storing them in the session object.
Slokun
Instead of `$_SESSION['locations']->listItems($_POST['companyLocation']);` try `unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']);`
Slokun
for the if statement, i haveunserialize($_SESSION['locations'])->listItems($_POST['companyLocation']);for the else statement, i have$_SESSION['locations'] = serialize(new Location()); unserialize($_SESSION['locations'])->listItems($_POST['companyLocation']);It's still not working, the array in the class, is not adding new records. It just keeps overwriting the old ones.
Idealflip