Welcome Guest, Not a member yet? Register   Sign In
Array in session
#1

[eluser]ThiagoPHX[/eluser]
Hello guys, i'm trying to add arrays in the sessions.

Like an e-commerce
i add one product
Code:
$this->session->set_userdata(array('product_id', $id));

Than, in other page if the user add a new product, i'll need to add this product_id too.

Thanks
#2

[eluser]Dam1an[/eluser]
You'd need to get the array as a var from the session, add the item to the array and then put it back
Here is a simplified example from a project I'm currently working on

Code:
$recent_hotels = $this->session->userdata('recent_hotels');
if($recent_hotels == false) {
    $recent_hotels = array($hotel_id);
} else {
    $recent_hotels[] = $hotel_id;
}
$this->session->set_userdata('recent_hotels', $recent_hotels);

If fetching the array from the session returns false, that key doesn;t exists, so we create the array, otherwise, just add to it, and then write it back to the session
#3

[eluser]Jay Logan[/eluser]
If you are simply trying to add an array to a session, you can do it by assigning the array to a variable, then add the variable to the session. IE:

Code:
$product_ids = array('1', '2', '3', '4');
$this->session->set_userdata('product_ids', $product_ids);
#4

[eluser]ThiagoPHX[/eluser]
[quote author="Dam1an" date="1250034642"]You'd need to get the array as a var from the session, add the item to the array and then put it back
Here is a simplified example from a project I'm currently working on

Code:
$recent_hotels = $this->session->userdata('recent_hotels');
if($recent_hotels == false) {
    $recent_hotels = array($hotel_id);
} else {
    $recent_hotels[] = $hotel_id;
}
$this->session->set_userdata('recent_hotels', $recent_hotels);

If fetching the array from the session returns false, that key doesn;t exists, so we create the array, otherwise, just add to it, and then write it back to the session[/quote]

Thanks !!!
Is exactly what i need.
#5

[eluser]Dam1an[/eluser]
You're welcome Smile




Theme © iAndrew 2016 - Forum software by © MyBB