CodeIgniter Forums
Store multiple "identical" arrays in a session - confused about variable/dynamic variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Store multiple "identical" arrays in a session - confused about variable/dynamic variables (/showthread.php?tid=33226)



Store multiple "identical" arrays in a session - confused about variable/dynamic variables - El Forum - 08-19-2010

[eluser]munkeh[/eluser]
I have a form that spans a few pages (well, technically it's a different form on each page but to the user it's a multi-stage form, I guess). The thing is, I need to grab all the data and do some validation before putting it all into the database at the end. Aha, you say - store it in a session! Well, I'm doing that but one of the forms is for the user to add information about a warranty part and there may be more than one part.

From browsing php.net I think I need to look into variable variables and while I understand the concept I can't wrap my head around it in practice.

Say I have a form that has a button "add part". I grab the POST data and build an array to store this in the session like so:
Code:
$add_parts = array(
                'form_description' => set_value('form_description'),
                'form_removed_part_number' => set_value('form_removed_part_number'),        
                'form_part_failure_date' => set_value('form_part_failure_date'),
                'form_work_order' => set_value('form_work_order')
            );
            
$this->session->set_userdata($add_parts);

The user is presented with a confirmation screen and has the opportunity to commit the data or add another part and it's this that I'm not clear on. I need to somehow figure out whether I've already added $add_parts to the session and, if so, increment some variable names and add it again while making note of the fact that there are multiple $add_parts arrays in the session.. I think?

I think I know what I need to do but I can't figure out how to do it! Help! Smile


Store multiple "identical" arrays in a session - confused about variable/dynamic variables - El Forum - 08-19-2010

[eluser]bretticus[/eluser]
Can't really wrap my head around why you mention variable variables. Smile

I think what you possibly meant is arrays inside arrays. Just build your add_parts array by inserting part meta data arrays inside your container array. You can use the part number as the associative array key of your container array elements. For example, if your controller method were called register:

Code:
function register()
{
    $this->load->library(array('session','form_validation'));
    $this->load->helper(array('form', 'url'));
                
    // form validation rules

    if ($this->form_validation->run() !== FALSE)
    {
        //get $addparts from previous post
        
        $add_parts = $this->session->userdata('add_parts');
        
        if ( $add_parts === FALSE )
            $add_parts = array(); // initialize container array.            
        
        //part number becomes associative array key within container array.
        $addparts[$this->input->post('form_removed_part_number', TRUE)] = array(
            'form_description' => $this->input->post('form_description', TRUE),
            'form_part_failure_date' => $this->input->post('form_part_failure_date', TRUE),
            'form_work_order' => $this->input->post('form_work_order', TRUE)
        );

        // add it back to session
        $this->session->set_userdata('add_parts', $add_parts);
        
        // do your logic about form completion and database insertion
    }
}



Store multiple "identical" arrays in a session - confused about variable/dynamic variables - El Forum - 08-19-2010

[eluser]munkeh[/eluser]
Well, I feel stupid, that makes total sense. I had initially tried to do what you suggested but I was getting errors from the Session library. My code didn't look like yours and it may have just been something as simple as a syntax error, so I then went off and tried to do it the hard way. Thanks for the help, I'm going to give that a try!


Store multiple "identical" arrays in a session - confused about variable/dynamic variables - El Forum - 08-19-2010

[eluser]bretticus[/eluser]
Oops, I made some mistakes with the session code because I wrote my example quickly. You'll want to store your parts data as a single item. I'll just correct the code above.


Store multiple "identical" arrays in a session - confused about variable/dynamic variables - El Forum - 08-19-2010

[eluser]munkeh[/eluser]
Yeah, I found that out when I tried to get the data back out of the session Smile

I was like 'wait a second, how am I supposed to know the session value before I look at the session value?'

So now I have a parts array in the session indexed by unique serial number..
Code:
$parts_array = $this->session->userdata('add_parts');
foreach($parts_array as $part)
{
    // do things to $part['removed_serial_number']
}
.. works just fine, problem solved and it's time for a coffee. Thanks again!