0
votes

I would like to store multiple values in cakephp session. Infact I like to store arrays in a session so I can keep track of products which user is adding into cart. I am trying to store values in session like;

$return_data = $this->Product->read(array('id','prod_name','prod_sku','prod_price'), $pid[0]); $this->Session->write('Cart', $return_data);

What happening here is I am getting data from database against the specific product and storing that information into session. the above code is working fine but with one exception and that is when some user adds 2nd product into cart the session overwrites the data of 1st product with 2nd product which is totally wrong. So now I need a way to add as many as products in session without overwriting other products data.

I am using CAKEPHP 1.3 frame work. WAMP for windows. PHP version: 5.3.4 MySQL: 5.1.53 Apache: 2.2.17

1

1 Answers

2
votes

You could try something like:

$count = count($this->Session->read('Cart')); // count the number of products you already have in the cart
// insert next:
$this->Session->write('Cart.'.$count, $this->Product->findById($pid[0], array('id','prod_name','prod_sku','prod_price')));

Notice the dot after "Cart", that gives you the array.