Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Mini Shopping Basket with only PHP4 Sessions
The Definitions
For the simplicity of this example I am using four separate arrays and some additional variables. The code can be optimized. This makes it easier to display the idea and to read the code. This could be done better with a class, but I am not quite sure if you can store objects into sessions. Someone comment on this?
We also need an item counter. Of course this could be done with the count() command, I just thought it would be nice to always know how many items there are, and it gives a nice counter for loops too.
The Works
Let's assume you have a listing of articles in your html page:
ID Name Price
1 Mouse 25.00 add
2 Key 100.00 add
3 Car 5000.00 add
4 Game 25.00 add

 
Adding the Link to Your Page
The 'add' field is the link to place items into the mini basket. Point this link to itself using the $PHP_SELF variable. Then add the article information to it. Here's the example for article 1.
<A HREF="<?echo $PHP_SELF;?>?id=1&price=25&basket=Mouse">add</A>
Article Names can contain spaces, so put it at the last position in your link. The get method seems to be picky about this.

 
Preparing the Minibasket
For reusability, lets create an external file minibasket.inc. I use the .inc extension to mark my include files. The reason to put this external is, even though you will be passing the basket on using session variables, the code still needs to be available.
The file will contain the code to display the mini basket, and the function to add items to it. Best place to implement this is to put the:
<?php include ("minibasket.inc"); ?>
command right there where you need it.

 
The Logic of the Minibasket.inc
Spend some time thinking about this. What should the minibasket look like, what features does it need? The minibasket displayed here would look something like this:
#NamePrice
1 Mouse 25.00
3 Game 75.00
Total 100.00
You can easily format this output using an external style sheet. It should not be too big, though. This minibasket is information, yet it is not the focus of the page you are showing.
The logic of the file is very simple.
Check if a new item needs to be added.
If true, add the item.
Within the adding it looks for duplicate entries and updates the existing entry by updating amount and pricing information.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
ShopArmen05/14/08 13:01
RE: latest software of PHPprosenjit das11/16/05 01:52
Php shop Shopping Basket Problem marc11/01/05 07:28
weird cart showing upkitaeshi05/02/05 08:57
RE: Shopping CartAsia04/28/05 13:30
need help with basketRob04/13/05 03:45
page indexing in array ashramsuswal03/01/05 09:30
why ????wapweb02/17/05 23:22
how to add to the basket using java servletsarshiya02/11/05 00:01
RE: Adding, Deleting, Updating. Examples:tania12/11/04 00:51
RE: Adding, Deleting, Updating. Examples:tania12/11/04 00:37
RE: final version?Jim Fletcher04/11/04 21:40
Subject: Updatet Script to become class Gerrit ZAAIJER12/29/03 19:52
Link Doesn't WorkEvan08/27/03 21:15
RE: Error?Carl08/12/03 08:53
RE: Adding, Deleting, Updating. Examples:Heikki07/25/03 04:02
Adding, Deleting, Updating. Examples:Ben06/22/03 17:54
final version?Termee04/17/03 15:27
RE: Anyone using this on a site yet?Evert03/31/03 06:14
RE: Anyone using this on a site yet?Carlton Fletcher03/24/03 15:45
RE: How to remove products from Basket ????Stephen Robinson02/06/03 05:19
RE: Cannot send session cache limiteStephen Robinson02/06/03 05:17
RE: Delete in minibasketStephen Robinson02/06/03 05:15
hot to emptry whole basketStephen Robinson02/06/03 05:12
RE: Inputting data into a databasebruc wells01/20/03 10:11
RE: Here an example how to uise the classMatt Pritchard01/07/03 04:11
Simplest PHP4 shoping basketJoão Barradas12/23/02 14:38
I converted the basket for PHP4.20 and higherMarco Simons12/06/02 08:08
RE: cart using session arrayspunknroll10/24/02 12:03
cart using session arraysBenno10/20/02 03:52
Example using Classlsimon10/19/02 13:38
updatecart=0Lindsey Simon10/18/02 16:12
Here an example how to uise the classEvert10/18/02 11:40
example using new basket classlsimon10/16/02 20:38
answers to include, delete reload etcEvert Smit10/10/02 10:09
Click reload button on browserJason 10/08/02 14:04
Evert.inc ??Jens10/08/02 09:02
Error?Jens10/08/02 08:57
RE: deleting items from basketevelyn10/04/02 05:39
RE: how do you change # of certain productEvert Smit09/26/02 13:05
how do you change # of certain productDEREK09/23/02 18:35
Updatet Script to become classEvert Smit09/16/02 04:44
Deleting Items Indivually from the CartAndrew09/05/02 06:56
RE: totaling upAndrew09/03/02 15:20
Demo shopfrode nystad08/12/02 20:06
dont forget to put the session_start() at theEli Vaknin07/25/02 19:52
Good scriptUnknown07/14/02 16:37
How to make a Recurring Billing System w/ PHPNick06/25/02 06:02
Code in useAntonio "Antimax" Araujo06/21/02 23:09
RE: Shopping Cartcliffdweller06/04/02 20:44
Shopping CartDebbie05/28/02 20:35
totaling upHargs05/03/02 06:21
RE: what is the html code to add item to cart!Hargs05/03/02 06:16
porting this example to a php class with OOEvert Smit02/22/02 04:45
RE: what is the html code to add item to cart!Evert Smit02/22/02 04:36
RE: empty cart added and solved refresh problem.Evert Smit02/22/02 04:34
what is the html code to add item to cart!Rydell Shah02/18/02 12:07
RE: deleting items from basketJoeyG02/14/02 19:46
Ordering SystemDonna02/13/02 10:12
RE: deleting items from basketJoeyG02/12/02 18:46
RE: deleting items from basketJoeyG02/06/02 19:14
RE: what about the reload buttonJoeyG02/06/02 19:05
RE: Why the code does not workmike02/03/02 04:12
RE: empty cart added and solved refresh problem.Andrew Salmons01/06/02 16:27
php session shams12/27/01 04:50
coping with session expirationBen12/04/01 19:12
empty cart added and solved refresh problem.Zane Amiralis11/21/01 18:43
RE: Anyone using this on a site yet?tfg110/26/01 09:50
Regarding this exampleEvert Smit10/20/01 16:07
RE: Bad Idea here - InsecureEvert10/20/01 15:59
RE: help me out with this one pleaseEvert10/20/01 15:54
RE: Inputting data into a databaseEvert10/20/01 15:52
RE: Displaying the basket on a different pageEvert10/20/01 15:49
RE: Session vars get reset!Ian10/15/01 18:13
Session vars get reset!Ian10/15/01 12:18
Displaying the basket on a different pageLuke10/02/01 21:38
PHP4 Session classG|ga10/02/01 06:40
RE: Why the code does not workxinkun09/25/01 12:17
DESTROY THE SESSIONloreto mateu09/04/01 11:01
Inputting data into a databaseTom Hemsley08/29/01 08:11
how difficult is it to put up correct codes??viren08/21/01 22:35
help me out with this one pleasejoe Bestler08/21/01 09:33
Delete in minibasketjeff07/05/01 13:41
RE: Why the code does not workJohn McGarry06/25/01 07:53
not runningDino05/17/01 10:22
RE: what about the reload buttonChunhui05/16/01 10:25
RE: Sessions how...Jeff Lambert05/15/01 21:29
RE: Bad Idea here - InsecureJeff Lambert05/15/01 21:26
Sessions... Further in online/offlineRichard05/10/01 08:29
what about the reload buttonPitter04/29/01 08:16
RE: Bad Idea here - InsecureOllie04/25/01 17:21
Make some cents!!!Simon Pritchard04/02/01 23:06
re: bad idea, insecurefandelem04/01/01 22:41
Bad Idea here - InsecureTodd03/19/01 11:55
Item names with multiple wordsAndy Lewis03/04/01 21:39
Anyone using this on a site yet?Mike Seiler03/04/01 12:03
Cannot send session cache limiteMarc Weller02/28/01 03:33
RE: Cannot add header informationDaniel02/27/01 07:16
How to remove products from Basket ????Neville02/25/01 18:43
RE: Warning: var. basket is undefinedJahu02/23/01 08:26
Warning: var. basket is undefinedVictoria Afonina02/20/01 16:31
deleting items from basketBrian02/05/01 10:07
Warnings when starting..Hans-Jörg Alles02/01/01 09:40
RE: Why the code does not workeveral rimbald01/21/01 09:53
Willing to buy prepackaged codeRon Kwok01/05/01 10:28
Posting to different page.brian01/04/01 18:55
Why the code does not workGreg Militelo01/02/01 16:07
Zip fileStephanie12/27/00 14:14
Howto check for session without creating one?Jed Baer12/22/00 11:51
RE: Not WorkingGreg Trepanier12/05/00 14:14
RE: Cookie securityKeith Davis11/20/00 19:55
About cut and pasteFernando10/24/00 15:52
Strange script... - part 2 (read the end!)Jens10/09/00 16:22
Not WorkingChris Toole09/11/00 23:16
Cannot add header informationRandy09/11/00 11:27
RE: Sessions how...Martin09/07/00 03:21
RE: Sessions how...nico09/01/00 19:12
Security problem!spencer p08/31/00 11:08
RE: Browser's Back ButtomVincent Lam08/29/00 03:05
RE: Why can't I get it works ?Evert Smit08/28/00 16:39
RE: Cannot send session ?? please helpJames Hsieh08/27/00 05:14
Why can't I get it works ?James Hsieh08/27/00 05:04
RE: Cannot send session ?? please helpEvert Smit08/25/00 04:32
RE: Sessions how...Evert Smit08/25/00 04:29
RE: Browser's Back ButtomVince08/24/00 17:15
RE: Browser's Back ButtomVince08/24/00 11:50
Sessions how...Martin08/23/00 16:23
RE: Cannot send session ?? please helpMartin08/23/00 16:16
Cannot send session ?? please helpGorazd Majcenovic08/23/00 15:07
RE: Browser's Back ButtomRichard Heyes08/20/00 07:56
RE: Cookie securityEvert Smit08/18/00 06:03
RE: Browser's Back ButtomEvert Smit08/18/00 06:00
Browser's Back ButtomManuel Durán Aguete08/18/00 05:08
RE: Cookie securityMartin Wheatley08/17/00 12:49
Cookie securityVesa Nippala08/17/00 01:30
Yes, you can store objects.Robert08/16/00 11:15
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.