471,596 Members | 810 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Problem with update function

133 100+
Hi,

I have a function which updates a cart but it is not working correctly. Basically if i have e.g.

2 x Red Shoes
3 x Blue Shorts

And then i change the qty of the blue shorts to 4 and pass to the update function it just completely removes the first lot of items in the list e.g. the 2 x red shorts.

The data is stored in a session like so:
Expand|Select|Wrap|Line Numbers
  1. 58:|Red Shoes|14.95,58:|Red Shoes|14.95,56:|Blue Shorts|8.95,56:Size|Blue Shorts|8.95,56:Size|Blue Shorts|8.95
Here is the code for the function:
Expand|Select|Wrap|Line Numbers
  1. case 'update':
  2.     if ($cart) {
  3.         $newcart = '';
  4.         foreach ($_POST as $key=>$value) {
  5.             if (stristr($key,'qty')) {
  6.                 $id = str_replace('qty','',$key);
  7.                 $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
  8.                 $newcart = '';
  9.                 foreach ($items as $item) {
  10.                     if ($id != $item) {
  11.                         if ($newcart != '') {
  12.                             $item = '';
  13.                             $newcart .= ','.$item;
  14.                         } else {
  15.                             $item = '';
  16.                             $newcart = $item;
  17.                         }
  18.                     }
  19.                 }
  20.                 for ($i=1;$i<=$value;$i++) {
  21.                     if ($newcart != '') {
  22.                         $newcart .= ','.$id;
  23.                     } else {
  24.                         $newcart = $id;
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.     }
  30.     $cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $newcart));
  31.  
  32.  
  33.     break;
  34. }
  35.  
Thanks in advance,
Adam
Sep 1 '09 #1
1 1280
Atli
5,058 Expert 4TB
Hi.

Why do you serialize the items into the session like that?

This would be much much easier to manage if you had each item in a separate session value. For example, a function to add items could look like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function addItem($itemID, $quantity) 
  3. {
  4.     if(!isset($_SESSION['chart'][$itemID])) {
  5.         $_SESSION['chart'][$itemID] = 0;
  6.     }
  7.     $_SESSION['chart'][$itemID] += $quantity;
  8. }
  9. ?>
  10.  
The $_SESSION variable is an array, and you can treat it as such.
The function just adds elements for each product, and stores the amount of that particular product you want to have in the chart.

Then, when you need to list the items in the chart, you just use a foreach loop on the $_SESSION['chart'] element.

I assume the price and other such info is listed in a database somewhere?
So there is no real reason to store that in the session to, is there?
Sep 2 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by junglist | last post: by
9 posts views Thread by zMisc | last post: by
2 posts views Thread by sorobor | last post: by
11 posts views Thread by Ed Dror | last post: by
4 posts views Thread by raghuvendra | last post: by
2 posts views Thread by swethak | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.