| ItalianRed99 2005-09-29, 6:17 pm |
| HI
I have completed the persistent shopping cart tutorial on this site and have
managed to adapt it to my requirements, although I have one problem I am trying
to overcome. I would like to add a field to my cart so it will hold my client
codes. Here is the code I have so far on my client page that users can add
clients to their cart.
Also the code in my cart page
I hope someone can help:)
<a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>">Add to
cart </a>
<?php
include("db.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
// Check if this item already exists in the users cart table
$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) values('" . GetCartId() .
"', $itemId)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId);
}
}
function UpdateItem($itemId)
{
// Updates the quantity of an item in the users cart.
// If the quantity is zero, then RemoveItem will be
// called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
if($qty == 20)
{
// Remove the item from the users cart
RemoveItem($itemId);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() .
"' and itemId = $itemId");
}
}
function RemoveItem($itemId)
{
// Uses an SQL delete statement to remove an item from
// the users cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and
itemId = $itemId");
}
function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$totalCost = 0;
$result = mysql_query("select * from cart inner join clients on cart.itemId
= clients.itemId where cart.cookieId = '" . GetCartId() . "' order by
clients.itemId asc");
?>
|