473,467 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Click button twice to update shopping cart?

Tarantulus
114 New Member
Hi Guys,

I'm creating a simple shopping cart app.

I have a button which launches $PHP_SELF? followed by variables (product id, qty, price etc)

the cart works fine, apart from updating the first purchase, which requires two button presses...

the code that is called when the button is pressed looks like this

Expand|Select|Wrap|Line Numbers
  1. if (!isset ($_SESSION['cart'])){
  2.             session_start();
  3.  
  4.  
  5.             if (isset ($_SESSION['cart'][$prod_id])){
  6.                 $_SESSION['cart'][$prod_id]['qty'] += $qty;
  7.             } 
  8.             else{
  9.                 $_SESSION['cart'][$prod_id] = array("qty"=>$qty,"price"=>$price);
  10.  
  11.             }
  12.         }
so in simple terms:

1 check if session is set, if not set it then move to next if
2. if the product is in the cart, just add one to it, otherwise, add the product to the cart with a in the qty requested.

Can you see anything wrong there??

thanks in advance
May 23 '08 #1
1 1643
ronverdonk
4,258 Recognized Expert Specialist
As your code is now, this only works when condition [php]if (!isset ($_SESSION['cart'])){[/php] is met.

You should start the session in the first statement if your script, i.e.[php]<?php
session_start();
....
[/php]Then you test whether the product exists in the session array and if so, increment it; if not add it to the session array.[PHP]
if (isset ($_SESSION['cart'][$prod_id]))
$_SESSION['cart'][$prod_id]['qty'] += $qty;
else {
$_SESSION['cart'][$prod_id]['qty'] = $qty;
$_SESSION['cart'][$prod_id]['price'] = $price;
}
[/PHP]Ronald
May 23 '08 #2

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

Similar topics

2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
1
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
9
by: Penny | last post by:
Hi all, I've built an online shopping cart using ASP Classic(based on a 'WebThang' tutorial). The shop cart page (with table showing customers selected items and costs) has only 3 buttons/links....
1
by: Alex Allage | last post by:
Hello, I am having a little problem. I have create a very basic shopping cart site. In the process of displaying items in the shop, I am using image buttons so that you may click on them and...
1
by: Zamael | last post by:
Okay... I've got a web app built in .NET 2.0 and for some reason I have to click a LinkButton twice in order for it to work. What's interesting is that the first URL reads as follows:...
1
by: TarDuk a memory beyond the life | last post by:
I am developing a shopping cart application for jwellary designing \ now i am displaying all the product in the table dynamically. and also show on image button as a add to cart after every item....
1
by: lisheen | last post by:
I am trying to create a type of shopping cart for a small site, I am using session variables. On the index page I have 3 items and when you click on buy you are taken to the shopping cart. The...
4
by: Shanthini Ganesh | last post by:
hi all. in my shopping cart project , i tested with two buttons, one for execution of client click and another for exection server method. these two buttons are as follows.. <input id="addbutton"...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.