Welcome Guest, Not a member yet? Register   Sign In
How to add dynamic array into session?
#1

[eluser]web_developer[/eluser]
How can push array values in Session array?

Currently At a time only one record is storing.

I want Session like below...
array_push($_SESSION['arrShopItem'],$arrAddItem);

But How can I do this in codeigniter?


Here is my Model Class Code..

Code:
class Shop_model extends Model
{
  var $arrShopItem=array();

  function Shop_model()
  {
    parent::Model();

    /** Load Necessary Libraries **/
    $this->load->library('session');
  }

  function add_product()
  {
    $arrAddItem=array();
    $arrAddItem['product_name']=$this->input->post('product_name');
    $arrAddItem['product_code']=$this->input->post('product_code');
    $arrAddItem['price']=$this->input->post('price');
    $arrAddItem['qty']=$this->input->post('qty');

    // Generate Session for new record //
    $this->session->set_userdata(array('arrShopItem' => $arrAddItem));

    // Destroy Temparory array Variable //
    unset($arrAddItem);

    return "";
  }
}
#2

[eluser]bigtony[/eluser]
Change this:
Code:
// change this
$this->session->set_userdata(array('arrShopItem' => $arrAddItem));

// To this:
$this->session->set_userdata('arrShopItem', $arrAddItem);
Then read it back like this:
Code:
$arrAddItem = $this->session->userdata('arrAddItem');
#3

[eluser]Phil Sturgeon[/eluser]
[quote author="bigtony" date="1257259394"]Change this:
Code:
// change this
$this->session->set_userdata(array('arrShopItem' => $arrAddItem));

// To this:
$this->session->set_userdata('arrShopItem', $arrAddItem);
Then read it back like this:
Code:
$arrAddItem = $this->session->userdata('arrAddItem');
[/quote]

That doesn't cover it entirely.

Code:
$arrShopItems = $this->session->userdata('arrShopItem');

$arrAddItem = array();
// populate the array with your data

$arrShopItems[] =& $arrAddItem;

$this->session->set_userdata('arrShopItem', $arrShopItems);

It's basically saying, get all the current items, add the new one, remember all of the items.

It's a bit of extra work, but dealing directly with Super-globals can lead to bad mojo.
#4

[eluser]stormbytes[/eluser]
I had a similar question and followed the example, but for some reason my sessions array has 2 entries for the array I'm trying to store. One called 'hold_asset_meta', while the other is called '0'. I'm debugging in Netbeans with Xdebug.

*FIXED -

t'was an older session where I did some testing. Deleted the session cookie & started over. Everything works as it should.

Wondering why the 'name-your-session-array' isn't documented in the UG.




Theme © iAndrew 2016 - Forum software by © MyBB