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 '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.
Article Names can contain spaces, so put it at the last position in your link. The get method seems to be picky about this.
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.
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:
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.
| Comments: |
| Shop | Armen | 05/14/08 13:01 |
| RE: latest software of PHP | prosenjit das | 11/16/05 01:52 |
| Php shop Shopping Basket Problem | marc | 11/01/05 07:28 |
| weird cart showing up | kitaeshi | 05/02/05 08:57 |
| RE: Shopping Cart | Asia | 04/28/05 13:30 |
| need help with basket | Rob | 04/13/05 03:45 |
| page indexing in array | ashramsuswal | 03/01/05 09:30 |
| why ???? | wapweb | 02/17/05 23:22 |
| how to add to the basket using java servlets | arshiya | 02/11/05 00:01 |
| RE: Adding, Deleting, Updating. Examples: | tania | 12/11/04 00:51 |
| RE: Adding, Deleting, Updating. Examples: | tania | 12/11/04 00:37 |
| RE: final version? | Jim Fletcher | 04/11/04 21:40 |
| Subject: Updatet Script to become class | Gerrit ZAAIJER | 12/29/03 19:52 |
| Link Doesn't Work | Evan | 08/27/03 21:15 |
| RE: Error? | Carl | 08/12/03 08:53 |
| RE: Adding, Deleting, Updating. Examples: | Heikki | 07/25/03 04:02 |
| Adding, Deleting, Updating. Examples: | Ben | 06/22/03 17:54 |
| final version? | Termee | 04/17/03 15:27 |
| RE: Anyone using this on a site yet? | Evert | 03/31/03 06:14 |
| RE: Anyone using this on a site yet? | Carlton Fletcher | 03/24/03 15:45 |
| RE: How to remove products from Basket ???? | Stephen Robinson | 02/06/03 05:19 |
| RE: Cannot send session cache limite | Stephen Robinson | 02/06/03 05:17 |
| RE: Delete in minibasket | Stephen Robinson | 02/06/03 05:15 |
| hot to emptry whole basket | Stephen Robinson | 02/06/03 05:12 |
| RE: Inputting data into a database | bruc wells | 01/20/03 10:11 |
| RE: Here an example how to uise the class | Matt Pritchard | 01/07/03 04:11 |
| Simplest PHP4 shoping basket | João Barradas | 12/23/02 14:38 |
| I converted the basket for PHP4.20 and higher | Marco Simons | 12/06/02 08:08 |
| RE: cart using session arrays | punknroll | 10/24/02 12:03 |
| cart using session arrays | Benno | 10/20/02 03:52 |
| Example using Class | lsimon | 10/19/02 13:38 |
| updatecart=0 | Lindsey Simon | 10/18/02 16:12 |
| Here an example how to uise the class | Evert | 10/18/02 11:40 |
| example using new basket class | lsimon | 10/16/02 20:38 |
| answers to include, delete reload etc | Evert Smit | 10/10/02 10:09 |
| Click reload button on browser | Jason | 10/08/02 14:04 |
| Evert.inc ?? | Jens | 10/08/02 09:02 |
| Error? | Jens | 10/08/02 08:57 |
| RE: deleting items from basket | evelyn | 10/04/02 05:39 |
| RE: how do you change # of certain product | Evert Smit | 09/26/02 13:05 |
| how do you change # of certain product | DEREK | 09/23/02 18:35 |
| Updatet Script to become class | Evert Smit | 09/16/02 04:44 |
| Deleting Items Indivually from the Cart | Andrew | 09/05/02 06:56 |
| RE: totaling up | Andrew | 09/03/02 15:20 |
| Demo shop | frode nystad | 08/12/02 20:06 |
| dont forget to put the session_start() at the | Eli Vaknin | 07/25/02 19:52 |
| Good script | Unknown | 07/14/02 16:37 |
| How to make a Recurring Billing System w/ PHP | Nick | 06/25/02 06:02 |
| Code in use | Antonio "Antimax" Araujo | 06/21/02 23:09 |
| RE: Shopping Cart | cliffdweller | 06/04/02 20:44 |
| Shopping Cart | Debbie | 05/28/02 20:35 |
| totaling up | Hargs | 05/03/02 06:21 |
| RE: what is the html code to add item to cart! | Hargs | 05/03/02 06:16 |
| porting this example to a php class with OO | Evert Smit | 02/22/02 04:45 |
| RE: what is the html code to add item to cart! | Evert Smit | 02/22/02 04:36 |
| RE: empty cart added and solved refresh problem. | Evert Smit | 02/22/02 04:34 |
| what is the html code to add item to cart! | Rydell Shah | 02/18/02 12:07 |
| RE: deleting items from basket | JoeyG | 02/14/02 19:46 |
| Ordering System | Donna | 02/13/02 10:12 |
| RE: deleting items from basket | JoeyG | 02/12/02 18:46 |
| RE: deleting items from basket | JoeyG | 02/06/02 19:14 |
| RE: what about the reload button | JoeyG | 02/06/02 19:05 |
| RE: Why the code does not work | mike | 02/03/02 04:12 |
| RE: empty cart added and solved refresh problem. | Andrew Salmons | 01/06/02 16:27 |
| php session | shams | 12/27/01 04:50 |
| coping with session expiration | Ben | 12/04/01 19:12 |
| empty cart added and solved refresh problem. | Zane Amiralis | 11/21/01 18:43 |
| RE: Anyone using this on a site yet? | tfg1 | 10/26/01 09:50 |
| Regarding this example | Evert Smit | 10/20/01 16:07 |
| RE: Bad Idea here - Insecure | Evert | 10/20/01 15:59 |
| RE: help me out with this one please | Evert | 10/20/01 15:54 |
| RE: Inputting data into a database | Evert | 10/20/01 15:52 |
| RE: Displaying the basket on a different page | Evert | 10/20/01 15:49 |
| RE: Session vars get reset! | Ian | 10/15/01 18:13 |
| Session vars get reset! | Ian | 10/15/01 12:18 |
| Displaying the basket on a different page | Luke | 10/02/01 21:38 |
| PHP4 Session class | G|ga | 10/02/01 06:40 |
| RE: Why the code does not work | xinkun | 09/25/01 12:17 |
| DESTROY THE SESSION | loreto mateu | 09/04/01 11:01 |
| Inputting data into a database | Tom Hemsley | 08/29/01 08:11 |
| how difficult is it to put up correct codes?? | viren | 08/21/01 22:35 |
| help me out with this one please | joe Bestler | 08/21/01 09:33 |
| Delete in minibasket | jeff | 07/05/01 13:41 |
| RE: Why the code does not work | John McGarry | 06/25/01 07:53 |
| not running | Dino | 05/17/01 10:22 |
| RE: what about the reload button | Chunhui | 05/16/01 10:25 |
| RE: Sessions how... | Jeff Lambert | 05/15/01 21:29 |
| RE: Bad Idea here - Insecure | Jeff Lambert | 05/15/01 21:26 |
| Sessions... Further in online/offline | Richard | 05/10/01 08:29 |
| what about the reload button | Pitter | 04/29/01 08:16 |
| RE: Bad Idea here - Insecure | Ollie | 04/25/01 17:21 |
| Make some cents!!! | Simon Pritchard | 04/02/01 23:06 |
| re: bad idea, insecure | fandelem | 04/01/01 22:41 |
| Bad Idea here - Insecure | Todd | 03/19/01 11:55 |
| Item names with multiple words | Andy Lewis | 03/04/01 21:39 |
| Anyone using this on a site yet? | Mike Seiler | 03/04/01 12:03 |
| Cannot send session cache limite | Marc Weller | 02/28/01 03:33 |
| RE: Cannot add header information | Daniel | 02/27/01 07:16 |
| How to remove products from Basket ???? | Neville | 02/25/01 18:43 |
| RE: Warning: var. basket is undefined | Jahu | 02/23/01 08:26 |
| Warning: var. basket is undefined | Victoria Afonina | 02/20/01 16:31 |
| deleting items from basket | Brian | 02/05/01 10:07 |
| Warnings when starting.. | Hans-Jörg Alles | 02/01/01 09:40 |
| RE: Why the code does not work | everal rimbald | 01/21/01 09:53 |
| Willing to buy prepackaged code | Ron Kwok | 01/05/01 10:28 |
| Posting to different page. | brian | 01/04/01 18:55 |
| Why the code does not work | Greg Militelo | 01/02/01 16:07 |
| Zip file | Stephanie | 12/27/00 14:14 |
| Howto check for session without creating one? | Jed Baer | 12/22/00 11:51 |
| RE: Not Working | Greg Trepanier | 12/05/00 14:14 |
| RE: Cookie security | Keith Davis | 11/20/00 19:55 |
| About cut and paste | Fernando | 10/24/00 15:52 |
| Strange script... - part 2 (read the end!) | Jens | 10/09/00 16:22 |
| Not Working | Chris Toole | 09/11/00 23:16 |
| Cannot add header information | Randy | 09/11/00 11:27 |
| RE: Sessions how... | Martin | 09/07/00 03:21 |
| RE: Sessions how... | nico | 09/01/00 19:12 |
| Security problem! | spencer p | 08/31/00 11:08 |
| RE: Browser's Back Buttom | Vincent Lam | 08/29/00 03:05 |
| RE: Why can't I get it works ? | Evert Smit | 08/28/00 16:39 |
| RE: Cannot send session ?? please help | James Hsieh | 08/27/00 05:14 |
| Why can't I get it works ? | James Hsieh | 08/27/00 05:04 |
| RE: Cannot send session ?? please help | Evert Smit | 08/25/00 04:32 |
| RE: Sessions how... | Evert Smit | 08/25/00 04:29 |
| RE: Browser's Back Buttom | Vince | 08/24/00 17:15 |
| RE: Browser's Back Buttom | Vince | 08/24/00 11:50 |
| Sessions how... | Martin | 08/23/00 16:23 |
| RE: Cannot send session ?? please help | Martin | 08/23/00 16:16 |
| Cannot send session ?? please help | Gorazd Majcenovic | 08/23/00 15:07 |
| RE: Browser's Back Buttom | Richard Heyes | 08/20/00 07:56 |
| RE: Cookie security | Evert Smit | 08/18/00 06:03 |
| RE: Browser's Back Buttom | Evert Smit | 08/18/00 06:00 |
| Browser's Back Buttom | Manuel Durán Aguete | 08/18/00 05:08 |
| RE: Cookie security | Martin Wheatley | 08/17/00 12:49 |
| Cookie security | Vesa Nippala | 08/17/00 01:30 |
| Yes, you can store objects. | Robert | 08/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.
|