LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2010, 08:51 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
How to write a shopping cart in PHP


Hi,
I'm learning PHP and I was curious about this. I've learnt about cookies and sessions, and I'd like to know - which one do you use for a shopping cart? What's the actual mechanism behind it? As the user builds up a cart full of stuff, is the data for that maintained on the server-side in a MySQL database or something?

Thanks.
 
Old 01-16-2010, 10:10 AM   #2
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
Maybe you can find this all at the side that is responsible for the Internet standard and how it have to be used at:
http://www.w3.org/

I only know the german projects for internet standard support at:
http://de.selfhtml.org/
and PHP at:
http://www.selfphp.de/

But think about if you want to use Perl, it is faster more stable, more flexible and individually.
You can also take cookies as option and store the cart over the IP-Address if cookies are not available, disabled, so you have more buyers.
 
Old 01-18-2010, 07:22 AM   #3
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Quote:
I've learnt about cookies and sessions, and I'd like to know - which one do you use for a shopping cart?
Cookies transmit the cookie data to and from the browser every time a request is made. This means that for a site with a lot of graphics and other files, if you've got a lot of cookie data, you could be adding quite a lot of extra bandwidth to your site.

Cookies are also insecure and can be modified by the browser user, so shouldn't be used for anything the needs to be kept secure.

PHP sessions use a cookie behind the scenes to track the sessionID, but all other session data is kept on the server.

Therefore, given the choice between cookies and PHP sessions, I would put most things into the PHP session, especially in a shopping basket scenario.

Keeping a set of shopping basket data in the session is relatively simple:
Code:
//Add a new item:
$_SESSION['basket'][]=array('product'=>$_POST['product'],'qty'=>$_POST['qty']);
(very much simplified; there's obviously more to it than that, but demonstrates the principle)

Quote:
As the user builds up a cart full of stuff, is the data for that maintained on the server-side in a MySQL database or something?
As you suggest, the best solution is to store the order data in a database as the user adds items to his basket. This is better than storing basket data in the PHP session because it gives your system a better audit trail of what's happening, and also it allows you to write a system such that a user can add items to his basket and come back later, log in, and they'll still be there.

This is obviously a lot more work than just adding it to the session array; you'll need to design your database tables and write the code to read and write to them. But you're going to need to do that anyway at some point, so may as well start here.

Cheers.
 
Old 02-02-2010, 01:40 AM   #4
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
Can you please explain this line? :

$_SESSION['basket'][]=array('product'=>$_POST['product'],'qty'=>$_POST['qty']);


What's got me foxed is the [] you put after the $_SESSION['basket'] - are you building a 2-Dimensional array or something? Why is that necessary?

Also, don't you need DOUBLE quotes for the 'product' key?




Thanks for all your help
 
Old 02-02-2010, 03:08 AM   #5
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Quote:
Originally Posted by resetreset View Post
Can you please explain this line? :

$_SESSION['basket'][]=array('product'=>$_POST['product'],'qty'=>$_POST['qty']);


What's got me foxed is the [] you put after the $_SESSION['basket'] - are you building a 2-Dimensional array or something? Why is that necessary?
The example was deliberately simplified, so I wasn't expecting you to use it exactly as shown, just as a starting point to try things out.

Yes, in the example, I'm building a 2-D array. (In fact, the way I've done it with the product/qty array it ends up being 3-Dimensional)

The empty square brackets tell PHP to create a new array element with the next available numeric key.

It isn't absolutely necessary in this example to create a multi-dimensional array since it doesn't have anything else, but if you wanted to use the session variable for anything else (eg user login, etc), then you'd need to keep the basket contents in their own array element.

In my example, I could have simplified it down one level like this:

[code]
$_SESSION['basket'][$_POST['product']]=$_POST['qty'];
[/quote]

...which would have done a similar job, but has the down-side that you could only have one entry for any given product in the basket (this may be what you want, but I'd prefer the flexibility of the other method even though the array is more complex)

Quote:
Also, don't you need DOUBLE quotes for the 'product' key?
No. PHP accepts single or double quotes for all strings.


Quote:
Thanks for all your help
No problem. Hope it all goes well.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a quick and easy shopping cart telakenesys Linux - Software 0 10-29-2008 05:26 PM
suggest a shopping cart ? nephish Linux - Software 3 12-18-2005 01:55 PM
shopping cart?? jme General 1 06-05-2004 10:07 AM
Shopping cart for linux graystarr Linux - Distributions 1 07-25-2002 04:42 PM
Specialized shopping cart system Jasutinsama Linux - General 2 01-11-2001 10:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration