473,373 Members | 1,141 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,373 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 1339
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.