hairynuggets
hairynuggets

Reputation: 3311

How to update an already set session with data?

I want to update a session array with the urls of the photos I have uploaded.

So far I have been able to create the session and add the array but I am unable to update the array and append further image urls to it.

How can I update a session with news data without overwriting the existing.

This is my code:

$show_photos = array('photo_url' =>'/assets/img/user_photos/thumbs/'.$image_data['file_name']);

$this->session->set_userdata('show_photos', $show_photos);

I am using the codeigniter framework but the same logic should still apply. http://codeigniter.com/user_guide/libraries/sessions.html

Upvotes: 0

Views: 1946

Answers (3)

dommel
dommel

Reputation: 56

You can use the php superglobal $_SESSION.

In your case $_SESSION['show_photos'] holds your array and you can simply append data using:

$_SESSION['show_photos'][] = array('your new data...');

Upvotes: 1

J0HN
J0HN

Reputation: 26921

The set_userdata method overwrites the session contents. So, to append to session var you'll have to explicitly do this:

//get old value
$old_val = $this->session->userdata('show_photos');
//append to it
$old_val[] = array('photo_url' =>'/assets/img/user_photos/thumbs/'.$image_data['file_name']);
//place it back
$this->session->set_userdata('show_photos', $old_val);

Upvotes: 5

Elen
Elen

Reputation: 2343

try:

 $_session['show_photos'][] =  $show_photos;

where $show_photos is the next element to array

Upvotes: 0

Related Questions