Home arrow MySQL arrow Page 4 - Building A Persistent Shopping Cart With PHP and MySQL
MYSQL

Building A Persistent Shopping Cart With PHP and MySQL


If you take a look around any PHP resource site, you'll notice an exorbitant amount of shopping cart scripts. The fact of the matter is that shopping cart scripts aren't hard to develop. In this article Mitchell shows us how to build a simple shopping cart that persists across multiple browser sessions. He uses PHP, MySQL, and JavaScript to implement each part of the shopping cart.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 214
May 18, 2002
TABLE OF CONTENTS:
  1. · Building A Persistent Shopping Cart With PHP and MySQL
  2. · Creating the database
  3. · Displaying the items
  4. · The cart.php script
  5. · The ShowCart function
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Building A Persistent Shopping Cart With PHP and MySQL - The cart.php script
(Page 4 of 6 )

A shopping cart must perform four functions. It must allow users to add and remove products from their cart. It must allow them to change the quantity of items for each product in their cart, and it must also allow them to actually see the products in their cart, as well as a cumulative total of all of the products in their cart.

Cart.php contains four functions that implement the work described in the previous paragraph. Cart.php requires on a query string variable, action, to tell it what to do:

<?php

include("db.php");

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}


As you can see, I'm using the $_GET associative array, which was introduced with PHP version 4.1, and made standard with PHP version 4.2. In PHP 4.2, $HTTP_GET_VARS is deprecated, so you should get into the habit of using $_GET and $_POST instead of $HTTP_GET_VARS and $HTTP_POST_VARS. Using $_GET is quicker too, because it is automatically globally scoped, whereas $HTTP_GET_VARS is not.

Looking at the switch statement above, we have four possible cases, each of which is discussed below:
  • add_item: When the user clicks on the "Add Item" for an item on the products.php page, this case will be called. It calls the AddItem function, passing in an items ID and quantity.
  • update_item: Updates the quantity of an item in the users shopping cart. As you will see shortly, each item in the cart is displayed with a drop down list, that, when changed, automatically updates the numeric of a specific item in the users shopping cart.
  • remove_item: Deletes an item from the cart table for the current user.
If the cart.php page is called up with no query string parameters, then the ShowCart function is called. Let's start by looking at the AddItem function.

AddItem accepts two parameters: The ID of the item to add to the cart, and the number of that item to add:

function AddItem($itemId, $qty)

The main part of the AddItem function checks whether or not this item already exists in the users cart. If it does, then its quantity field is updated and it isn't added again:

$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead

UpdateItem($itemId, $qty);
}


Looking at the code above, we can see that if $numRows equals zero (i.e. the item isn’t already in the users cart) then the item is added to the cart table. If not, the items quantity field is updated by calling the UpdateItem function, which is described below.

UpdateItem accepts two parameters, in the same way that the AddItem function does:

function UpdateItem($itemId, $qty)

It executes a simple UPDATE SQL query against the cart table, updating the quantity of one specific item. The cookieId field is used to match the users session ID to that particular product, making sure that the quantity is only updated for that item and the current user:

mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");

Removing an item is a simple matter of the RemoveItem function being called. It accepts just one parameter, which is the ID of the item to delete:

function RemoveItem($itemId)

Once connected to the database, a simple SQL DELETE query removes the item from the current users cart:

mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

All of these functions are good, but to actually call them, we need to look at the ShowCart function, which is what we will do on the next page.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 – More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials