• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding to existing session

#1
[eluser]nuttynibbles[/eluser]
how do i add data to existing session.
e.g:
the current session contain this...
$sessiontag=array('sessid'=>'1234abc','a1'=>'apple','a2'=>'boy');

and now i wanna add another value 'a3'=>'cat' into the session array, how can i approach this problem.

#2
[eluser]Pascal Kriete[/eluser]
Umm.. set_userdata() ?

Try out the user guide, it really is quite useful sometimes.

#3
[eluser]nuttynibbles[/eluser]
okie this is what i did.

1. i created a session in page1.php
Code:
$sessiontag=array('sessiontag'=>array('sessiontagid'=>'abc123')

2. then page1.php will be redirected to page2.php

3. in page2.php, i have a button to insert session values of unique key and the new value. this is what i did:

Code:
/*controller*/
//when button is pressed to insert new value
if($this->input->post('postvalue')=="2"){
//generate unique id
$uniqueid=uniqueid(date('Y-m-d h:i:s:u'),'helloworld');
insertsession('sessiontag',$this->session->userdata('sessiontag'),$uniqueid);
}

/*external function*/
function insertsession($sessid="",$sessionarray="",$uniqueid="")
{
if(!$sessid && !$sessionarray && !$uniqueid)
  return;
          
$sessionnew=array();

foreach($sessionarray as $k=>$v){
//dont overwrite sessiontagid = abc123
if($k=='sessiontagid'){
   $sessionnew[$k]=$v;
   continue;              
  }
//update the existing value
if($k==$uniqueid)
  $sessionnew[$k]="update value";
//insert new array value to session
if($k!=$uniqueid)
  $sessionnew[$uniqueid]="new value";
}
$sessionnew=array('sessiontag'=> $sessionnew);
$this->session->set_userdata($sessionnew);
}

3. each time i did an insert, i realized the unique key and value were overwritten. it did not add a new array. why is this so?

thanks help.

#4
[eluser]nuttynibbles[/eluser]
anyone with any idea?

#5
[eluser]frietkot[/eluser]
You could get the data from the existing session into an array and then push the new data into that array.
Next step is to make a new session with the new array with all the data in it.

Something like that?

#6
[eluser]nuttynibbles[/eluser]
yup that was what i did. I took out the array and insert new value. But instead of adding new row of value, it overwrite the previous value.

the above code i posted is what i have done.

#7
[eluser]Randy Casburn[/eluser]
[quote author="nuttynibbles" date="1223499079"]yup that was what i did. I took out the array and insert new value. But instead of adding new row of value, it overwrite the previous value.

the above code i posted is what i have done.[/quote]

Wow....This is much less a CI session thing than a basic array thing...

@nutty -- have you ever heard of array_pop, array_push, array_shift, and array_unshift ?

Code:
/*controller*/
//when button is pressed to insert new value
if($this->input->post('postvalue')=="2"){
  //generate unique id
  $uniqueid=uniqueid(date('Y-m-d h:i:s:u'),'helloworld');
  $newSession = ($this->session->userdata('sessiontag') != '' )? $this->session->userdata('sessiontag') : array() ;
  array_push($newSession, $uniqueid);
  $this->session->set_userdata('sessiontag',$newSession);
}

You can test this as I'm not exactly sure this will work, but this will get you darn close.

Try this please.

Randy

#8
[eluser]frietkot[/eluser]
Thats what i ment to say Randy. ThanksWink

#9
[eluser]Randy Casburn[/eluser]
[quote author="frietkot" date="1223555510"]Thats what i ment to say Randy. ThanksWink[/quote]


Ahh...I see. I put up some code just so nutty could see the difference. I hope it was helpful.

Randy

#10
[eluser]nuttynibbles[/eluser]
hey both, i managed to dynamically add values to array. however, i encounter some problem while doing so. because this function is done when im login, in the process, i realise my session got destroyed and i was logout of the system. im not sure why but i am still investigating.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.