Welcome Guest, Not a member yet? Register   Sign In
Multi dimensional array in Sessions

I'm hoping someone will be able to help me with this. I've been getting into CI and I'm trying to use it on a new site I'm working on. Mostly things are going ok (very well in fact), but I've hit a problem with sessions. With traditional php I store a shopping cart in this way:

$_SESSION['shopcart'][$productid] = $productqty;

Or something like that. It gives something like:

$_SESSION['shopcart'][9483] = 4;
$_SESSION['shopcart'][3898] = 20;
$_SESSION['shopcart'][2334] = 1;

So I build up a session array that I can easily run through using a foreach:

foreach ($_SESSION['shopcart'] as $key => $value)
.... etc

I'm not sure how to get this working with CI, every time I tried (and I did try different approaches) I ended up with only one key/value combination in the array, it just overwrote it every time. So the first time through the loop I would end up with:

$_SESSION['shopcart'][9483] = 4;

But the second time through I would end up with:

$_SESSION['shopcart'][3898] = 20;

CI sessions would not keep the original session value.

I'm sure there must be a way to do this, any suggestions would be welcome.

Thanks in advance...

[eluser]Ebot Ndip-Agbor[/eluser]
this thread might help: http://ellislab.com/forums/viewthread/70536/

You can use serialize() before storing the data and unserialize() when you retrieve it. Using these two methods you can store any type of data you like including complex types (e.g. An array of Objects).

Serialize isn't necessary here at all, it does nothing for him.

Can you post the rest of your code. The code you're having problems with isn't in your message. Obviously in your loop you're accidentally overwriting the values.

First, I'd second Negligence's suggestion for the quickest path to a solution.

FWIW I'm utilizing the Session class. For simplicity, I'm putting a many-dimensional array into one index of the session array:
$this->session->set_userdata('datafile', $xls_return);

Later I retrieve it:
$datafile = $this->session->userdata('datafile');
Then start looping over the pertinent section of $datafile. In our case, there's a 2-dimensional array under $datafile["cell"] that contains the content of an Excel input file. Therefore, I never even think about updating the session with individual bits of the array, even in a loop. Just read and write the whole thing at once. Works like a champ.

I'm using the OBsession addition (with CI 1.6.1), but that is probably irrelevant to this thread. Further irrelevant to this discussion is the use of Excel Parser Professional by Algera - it's just a nice little tool that works well for us.

Theme © iAndrew 2016 - Forum software by © MyBB