473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1340
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

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

Similar topics

4
by: Derek | last post by:
Hi, I've built a rather large CGI that dumps a lot of data and a fairly complex javascript app out to the client's browser. Granted this may be poor style according to someone web design...
2
by: enio | last post by:
Hi, I intend to get some answer from postgres concerning if an update command has altered some records in the database. To do this I coded this function: create or replace function...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
9
by: zMisc | last post by:
When I try to update record, I kept getting this error: Row cannot be located for updating. Some values may have been changed since it was last read. No other users are accessing the database...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
4
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
1
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.