Welcome Guest, Not a member yet? Register   Sign In
Session - Array of Array
#1

[eluser]soprabaixar1[/eluser]
How do I work with array of array inside the session? For example, I have an array of various products.

There is a button that when you click add these products in the session array, and the limit are up to six products.

I just want to add more products in the session array and I can not. This array structure is actually just one example of what I really need. I'm doing so:
Code:
$products[] = array(
   'ID_TYPE_SERVICE'            => $ID_TYPE_SERVICE,
   'FL_PA'                      => $FL_PA,
   'ID_AR'                      => $ID_AR,
   'ID_SA'                      => $ID_SA,
   );

$session_data = array(
  "products" => $products
  );
$this->session->set_userdata($session_data);



// Debug - print_r
Array
(
    [session_id] => 504e0dec0b13dcec9852246469700657
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0
    [last_activity] => 1334954836
    [ID_AA] => Array
        (
            [0] => 26
        )

    [ID_BB] => Array
        (
            [0] => 33207
        )

    [products] => Array
        (
            [0] => Array
                (
                    [ID_TYPE_SERVICE] => Array
                        (
                            [0] => 2
                        )

                    [FL_PA] => 1
                    [ID_AR] => 4
                    [ID_SA] => 197
                )
        )
)
#2

[eluser]WanWizard[/eluser]
Are you using the database or the cookie for your session?

Cookies are very limited in size, if you store array structures in it, you run into the size limits quite easily.
#3

[eluser]soprabaixar1[/eluser]
I'm not using database. Do not understand very well.

The structure of the array is small. I think it would have no problem in size.
#4

[eluser]WanWizard[/eluser]
You're talking about a "button that adds...", that is application specific.
So if the application restricts you to 6 items, perhaps you have to modify that?
#5

[eluser]soprabaixar1[/eluser]
WanWizard,
Correct, this value (6) could be need to be modified. Researching, I found a comment in the forum talking about the class "cart" of the CodeIgniter and I'm doing some tests here using this algorithm:

http://ellislab.com/codeigniter/user-gui.../cart.html
(The Adding Multiple Items to Cart)

I did what I needed in part. The problem I am having is adding a new item in the array, I am using the function "array_push ()" PHP is that before I read the array in this session.

But the problem that the structure of the array was broken, probably the logic I'm using.
Not keeping the level of the structure of the array, the level of the array is coming down.
#6

[eluser]soprabaixar1[/eluser]
I got, but how can I delete for example products['2'] and products['4']?
This is not working:

Code:
$this->session->unset_userdata("products['2']");
#7

[eluser]soprabaixar1[/eluser]
Resolved!!

The CI should support multi array delete the session, but from what I see does not support.

The way you delete it as follows:

Code:
unset($this->session->userdata['products']['2']);
$this->session->sess_write();

I got following the algorithm of the function "unset_userdata" of WanWizard. On post: http://ellislab.com/forums/viewthread/158206/#763169

For add more products in the session array just follow the same logic above of the post #4 this topic.
#8

[eluser]soprabaixar1[/eluser]
We really had a problem with the size and for resolve only changed the configuration of the session to work with database and created a table in the database, as follows documentation.




Theme © iAndrew 2016 - Forum software by © MyBB