CodeIgniter Forums
Update session data with new values but keep old ones - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Update session data with new values but keep old ones (/showthread.php?tid=34859)



Update session data with new values but keep old ones - El Forum - 10-11-2010

[eluser]PHP Creative[/eluser]
Hi there

I have the following code. It updates the session data with the an array from the table.

Code:
$this->db->select('ID');        
        $this->db->from('FoodTable');
        $this->db->where('Food',$this->input->post('food'));    
        $query = $this->db->get();
        
        

        
            $data = array(
                   'FoodID'  => $query->result_array()
               );

            $this->session->set_userdata($data);

What I want to do is keep the old values and add the new ones to the session data. For example a user comes along, searches for something, adds to the session, then searches for another thing. Both searches are included in session so I can display in a table.

The above code only keeps the new session and not the old data as it is overwritten.

Any help or advice would be much appreciated.

Thanks a millon


Update session data with new values but keep old ones - El Forum - 10-11-2010

[eluser]theprodigy[/eluser]
Just store your food ids as an array in session (I would use database session rather than cookie):

Code:
$this->db->select('ID');
$this->db->from('FoodTable');
$this->db->where('Food',$this->input->post('food'));    
$query = $this->db->get();

$food_ids = $this->session->userdata('FoodID');

if( ! $food_ids )
{
   $food_ids = array();
}

$food_ids[] = $query->result_array();

$data['FoodID'] = $food_ids;

$this->session->set_userdata($data);



Update session data with new values but keep old ones - El Forum - 10-12-2010

[eluser]PHP Creative[/eluser]
Thanks very much for that theprodigy. I appreciate it.

Regarding using database sessions, what are the benefits over cookies?

Thanks a million.


Update session data with new values but keep old ones - El Forum - 10-12-2010

[eluser]theprodigy[/eluser]
The difference is size. Cookies are allowed a maximum of 4KB of data. I'm not sure what restrictions are on the database session, but it's larger than 4KB.
Also, if you are going to go this route, don't forget to allow an option for the user to be able to remove previous searches as well ;-)


Update session data with new values but keep old ones - El Forum - 10-12-2010

[eluser]WanWizard[/eluser]
The size of the userdata when using database sessions is limited by the column type used for the column 'userdata'. If you take LONGTEXT, you have 2^32 bytes of storage per session record...


Update session data with new values but keep old ones - El Forum - 10-12-2010

[eluser]theprodigy[/eluser]
If you are using the table as specified by CI in the sessions user_guide page, it is set to text (not longtext), and therefore has 2^16 bytes (according to mysql's docs). You can, of course, change this if your requirements deem necessary, but that is the default.