the tutorial is a bit itsy bitsy but im pretty sure i got it all. here it is is anyone wants to see:
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:
Code: Select all
<?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.
At this point we need a way to list all of the items in our shopping cart, as well as a total cost for all of those items. ShowCart handles all of this. It also provides a drop down list for each item, so that its quantity can be easily updated.
ShowCart starts off by using an SQL INNER JOIN query to get a list of items from the cart table, and also each items details from the items table:
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
Once the list of items is retrieved, each item is displayed as part of a table, as a table row:
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}
The running tally of each item in the users shopping cart is kept as the $totalCost variable:
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
The quantity of each item is multiplied by its price, and is then added to the $totalCost variable using the += operator, which is the same as:
$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]);
As I mentioned above, the quantity for each item is displayed in a drop down list. This list is generated with the following code:
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
The <select> tag contains a trigger for the onChange event, which calls a JavaScript function called UpdateQty. UpdateQty is defined at the top of the ShowCart function, and looks like this:
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
Once called, UpdateQty takes the quantity of the selected item as well as the items ID (which is the name of the drop down list) and passes them as query string variables to cart.php. For example, if I updated the quantity of a product whose ID was 5 to 4, then I would be redirected to the following URL:
cart.php?action=update_item&id=5&qty=4
Lastly, ShowCart displays the total cost of all items in the users cart:
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
[