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.
Next: The ShowCart function >>
More MySQL Articles
More By Mitchell Harper
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|