vikash
vikash

Reputation: 475

using set_userdata() in codeigniter replaces the previous session value

I am trying to merge previous session value with current session value but unfortunately the last session are destroy when i try to merge both value.

this is the code i use for merge session array.

$sess=$this->session->userdata('sesse');
    $covr_wrap_mil=array();
                $covr_wrp=$this->session->userdata('bar');              
                $mil=array('miluna_products'=>$milunaid,'total_price'=>$totalprice);            
                $covr_wrap_mil[]=array_merge($covr_wrp,$mil);

                    if(isset($sess)  && !empty($sess)):

                        $oldses=$this->session->userdata('sesse');                      
                                $oldses=array_merge($oldses,$covr_wrap_mil);                            
                                $this->session->set_userdata('sesse',$covr_wrap_mil);

                    else:

                        $this->session->set_userdata('sesse',$covr_wrap_mil);   

                    endif;

currently i am getting this array

 Array
(
    [0] => Array
        (
            [style_id] => 308
            [wrap] => Array
                (
                    [285] => 285
                )

            [cover] => Array
                (
                    [307] => 307
                )

            [miluna_products] => a:2:{s:6:"322%14";s:30:"a:1:{i:0;s:12:"100_2102.jpg";}";s:7:"323%268";s:35:"a:1:{i:0;s:17:"1449004825736.gif";}";}
            [total_price] => 282
        )

)

but i want to merge newly array with previous array like this.

    Array
(
    [0] => Array
        (
            [style_id] => 308
            [wrap] => Array
                (
                    [285] => 285
                )

            [cover] => Array
                (
                    [307] => 307
                )

            [miluna_products] => a:2:{s:6:"322%14";s:30:"a:1:{i:0;s:12:"100_2102.jpg";}";s:7:"323%268";s:35:"a:1:{i:0;s:17:"1449004825736.gif";}";}
            [total_price] => 282
        )
    [1] => Array
        (
            [style_id] => 309
            [wrap] => Array
                (
                    [275] => 275
                )

            [cover] => Array
                (
                    [377] => 377
                )

            [miluna_products] => a:2:{s:6:"322%14";s:30:"a:1:{i:0;s:12:"100_2102.jpg";}";s:7:"323%268";s:35:"a:1:{i:0;s:17:"1449004825736.gif";}";}
            [total_price] => 282
        )

)

not understand where is the problem. any help would highly appreciated.

Upvotes: 1

Views: 519

Answers (3)

Akshay Hegde
Akshay Hegde

Reputation: 16997

You have merged array but you missed to set session data with merged array,

Change

Here you are overwriting session with $covr_wrap_mil data instead of merged array $oldses data so it was replacing old session data

$oldses=$this->session->userdata('sesse');                               
$oldses=array_merge($oldses,$covr_wrap_mil);  
/* Here you are overwriting session with covr_wrap_mil data 
   instead of merged array data so it was replacing old session   
   data
*/                        
$this->session->set_userdata('sesse',$covr_wrap_mil);

To

$oldses=$this->session->userdata('sesse');                               
$oldses=array_merge($oldses,$covr_wrap_mil);                          
$this->session->set_userdata('sesse', $oldses);

Finally It would look like below

 $sess=$this->session->userdata('sesse');

 $covr_wrap_mil=array();
 $covr_wrp=$this->session->userdata('bar');              
 $mil=array('miluna_products'=>$milunaid,'total_price'=>$totalprice);            
 $covr_wrap_mil[]=array_merge($covr_wrp,$mil);

 if(!empty($sess)):   
     $oldses=$sess;                      
     $oldses=array_merge($oldses,$covr_wrap_mil);                            
     $this->session->set_userdata('sesse',$oldses);   
 else:
     $this->session->set_userdata('sesse',$covr_wrap_mil);   
 endif;

Upvotes: 1

Deep 3015
Deep 3015

Reputation: 10075

use array_push() instead of array_merge().In array_merge() keys are updated with the newer values and you lost earlier session values

So your code will be

$sess=$this->session->userdata('sesse');
$covr_wrap_mil=array();
            $covr_wrp=$this->session->userdata('bar');              
            $mil=array('miluna_products'=>$milunaid,'total_price'=>$totalprice);            
            $covr_wrap_mil[]=array_merge($covr_wrp,$mil);

                if(isset($sess)  && !empty($sess)):
                 $older_session=array(); //crate new array
                 $oldses=$this->session->userdata('sesse');                      
                 array_push($older_session,$oldses,$covr_wrap_mil);
               /*update session with $older_session*/                           
                 $this->session->set_userdata('sesse',$older_session);

                else:

                    $this->session->set_userdata('sesse',$covr_wrap_mil);   

                endif;

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

You are doing right. Only change one line

 $oldses=$this->session->userdata('sesse');                      
 $oldses=array_merge($oldses,$covr_wrap_mil);                            
 $this->session->set_userdata('sesse',$oldses);

to set mergred array to session you have to store value of $oldses not of $covr_wrap_mil

Upvotes: 0

Related Questions