First I found this http://www.zend.com/codex.php?id=112&single=1
And then I also came across this
Code: Select all
<?
class cart {
var $username = "username";
var $password = "password";
var $database = "your_db";
var $hostname = "localhost";
var $inv_table = "inventory";
var $cart_table = "shopping";
function cart($cart_id) {
$this->dblink = mysql_connect($this->hostname, $this->username, $this->password);
mysql_select_db($this->database, $this->dblink);
$this->cart_id = $cart_id;
}
function add_item($product, $color, $quantity) {
$qty = $this->check_item($product,$color);
if($qty == 0) {
$query = "INSERT INTO ".$this->cart_table . " (session, product, color, quantity) VALUES ('".$this->cart_id."', '$product', '$color', '$quantity') ";
mysql_query($query, $this->dblink);
} else {
$quantity += $qty;
$query = "UPDATE ".$this->cart_table . " SET quantity='$quantity' WHERE session='".$this->cart_id."' AND product='$product' AND color='$color' ";
mysql_query($query, $this->dblink);
}
return true;
}
function check_item($product, $color) {
$query = "SELECT quantity FROM ".$this->cart_table. " WHERE session='".$this->cart_id."' AND product='$product' AND color='$color' ";
$result = mysql_query($query, $this->dblink);
if(!$result) {
return 0;
}
$numRows = mysql_num_rows($result);
if($numRows != 0) {
$row = mysql_fetch_object($result);
return $row->quantity;
}
}
function delete_item($product, $color) {
$query = "DELETE FROM ".$this->cart_table." WHERE session='" . $this->cart_id."' AND product='$product' AND color='$color' ";
mysql_query($query, $this->dblink);
}
function clear_cart() {
$query = "DELETE FROM ".$this->cart_table." WHERE session='".$this->cart_id."' ";
mysql_query($query, $this->dblink);
}
function modify_quantity($product, $color, $quantity) {
if($quantity <= 0) return $this->delete_item($product);
$query = "UPDATE ".$this->cart_table. " SET quantity='$quantity' WHERE session='".$this->cart_id."' AND product='$product' AND color='$color'";
mysql_query($query, $this->dblink);
return true;
}
function get_contents() {
//get stuff in the cart and return in indexed array
$q1 = "SELECT * FROM ". $this->cart_table. " WHERE session='".$this->cart_id."'";
$incart_r = mysql_query($q1,$this->dblink) or die(mysql_error());
$contents = array();
while($incart = mysql_fetch_array($incart_r)){
//get the item's price first
$q2 = "SELECT price FROM ". $this->inv_table. " WHERE product='".$incart["product"]."'";
$price_row = mysql_query($q2) or die(mysql_error());
$price = mysql_fetch_row($price_row);
//build array of info
$item = array($incart['product'],$incart['color'],$incart['quantity'],$price[0]);
array_push($contents,$item);
}
return $contents;
}
function cart_total() {
$contents = $this->get_contents();
$total = 0;
for($i=0; $i < count($contents); $i++){
$total = $total + ($contents[$i][3] * $contents[$i][2]);
}
$total = "$".number_format($total, 2, '.', '');
return $total;
}
function quant_items() {
$contents = $this->get_contents();
$q = 0;
for($i=0; $i < count($contents); $i++){
$q = $q + $contents[$i][2];
}
return $q;
}
}
?>Code: Select all
<?
// Cart Cookie Creator //////////////////////////
if(!isset($HTTP_COOKIE_VARS['cart_id'])) {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 14400);
} else {
$cart_id = $HTTP_COOKIE_VARS['cart_id'];
}
/////////////////////////////////////////////////
// Make the Cart Object /////////////////////////
require_once("shoppingcart.php");
$cart = new cart($cart_id);
/////////////////////////////////////////////////
// Select Action ////////////////////////////////
switch($_GET['a']){
case "add":
if(isset($_POST['p']) and isset($_POST['c']) and isset($_POST['q'])) $cart->add_item($_POST['p'],$_POST['c'],$_POST['q']);
break;
case "update":
$cart->modify_quantity($_POST["p"],$_POST['c'],$_POST["q"]);
break;
case "delete":
$cart->delete_item($_POST['p'],$_POST['c']);
break;
case "clear":
$cart->clear_cart();
break;
}
///////////////////////////////////////////////
// Begin Cart Display /////////////////////////
$c = $cart->get_contents();
$cart_tbl = "
<!-- Begin Cart Display -->
<table summary='Your shopping cart' id='cart'>
<tr>
<th>Item</th><th>Color</th><th>Quantity</th><th>Price</th><th> </th>
</tr>";
if(count($c) <= 0){ //nothing in cart
$cart_tbl .= "
<tr>
<td class='items' colspan='5'>Your cart is empty</td>
</tr>";
}else{ //stuff in the cart so show it
for($i=0; $i < count($c); $i++){
$cart_tbl .= "
<tr>
<td class='items'>" . $c[$i][0] . "</td>
<td class='items'>" . $c[$i][1] . "</td>
<td class='items'>
<form action=\"showcart.php?a=update\" method=\"POST\">
<input type='hidden' name='p' value='". $c[$i][0] ."' />
<input type='hidden' name='c' value='". $c[$i][1] ."' />
<input type='text' name='q' size='3' value='" . $c[$i][2] . "' />
<input type='submit' value='update' />
</form>
</td>
<td class='items'>$". $c[$i][3] ."</td>
<td class='items'>
<form action=\"showcart.php?a=delete\" method=\"POST\">
<input type='hidden' name='p' value='". $c[$i][0] ."' />
<input type='hidden' name='c' value='". $c[$i][1] ."' />
<input type='submit' value='remove item' class='delitem_btn' />
</form>
</td>
</tr>";
}
}
$cart_tbl .= "
<tr>
<td colspan='3' class='empty'> </td><td class='total'>". $cart->cart_total() ."</td><td class='items'><form action=\"showcart.php?a=clear\" method=\"post\"><input type=\"submit\" value=\"clear cart\" /></form></td>
</tr>";
$cart_tbl .= "
</tr>
</table>
<!-- End Cart Display -->\n\n\n";
/////////////////////////////////////////////////
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Your Shopping Cart</title>
<style type="text/css" media="all">
/* Some Clunky Style Rules To Be Highly Modified */
#cart
{width:auto;}
#cart th
{padding:5px 15px 5px 15px;
font-family:Arial, Helvetica, sans-serif;
background-color:#999;
color:#000;
border:1px solid #ccc;
text-transform:uppercase;}
#cart .items
{padding:5px 15px 5px 15px;
font-family:Arial, Helvetica, sans-serif;
background-color:#ccc;
color:#333;
border:1px solid #999;
vertical-align:middle;}
#cart .empty
{background-color:#fff;}
#cart .total
{padding:5px 15px 5px 15px;
font-family:Arial, Helvetica, sans-serif;
background-color:#999;
color:#333;
border:1px solid #fff;}
#minicart
{width:auto;
border:1px solid #333;
padding:0px;
background-color:#ccc;}
.minihead
{padding:5px 15px 5px 15px;
font-family:Arial, Helvetica, sans-serif;
background-color:#999;
color:#000;
border:1px solid #ccc;
text-transform:uppercase;
text-align:center;}
.minicontent
{padding:5px 15px 5px 15px;
font-family:Arial, Helvetica, sans-serif;
background-color:#ccc;
color:#333;
border:1px solid #999;
text-align:center;}
</style>
</head>
<body>
<?
echo $cart_tbl;
$t = $cart->cart_total();
$qty = $cart->quant_items();
echo "
<p>
<!-- Begin Mini Cart Display -->
<!-- Note: Put this mini cart on all ecommerce pages except showcart.php. -->
<!-- It is just here for demonstration and testing purposes -->
<table summary='Your shopping cart' id='minicart'>
<tr>
<td class='minihead' colspan='2'>Your Shopping bag</td>
</tr>
<tr class='minicontent'>
<td>" . $qty . " items</td><td>" . $t . "</td>
</tr>
</table>
<!-- End Mini Cart Display -->
</p>\n\n\n";
?>
<br />
<br />
<!-- Sample Cart Interaction Buttons (Add item buttons would normally be on product pages) -->
<p><form action="showcart.php?a=add" method="post">
<input type="submit" value="add widget" />
<input type="hidden" value="widget" name="p" />
<input type="hidden" value="green" name="c" />
<input type="hidden" value="1" name="q" /></form></p>
<p><form action="showcart.php?a=add" method="post">
<input type="submit" value="add widget 2" />
<input type="hidden" value="widget2" name="p" />
<input type="hidden" value="red" name="c" />
<input type="hidden" value="1" name="q" /></form></p>
<!-- End Sample Cart Interaction Buttons -->
</body>
</html>now here is the code for my form
Code: Select all
<form name="cart_quantity" method="POST" style="display:inline; margin:0px;" autocomplete = "off">
<div class="bttns"><input type="image" src="i/update_quote.gif" border="0" alt="add" ></div>
<a name="quote"></a><div class="h2brdr"><h2>Current Quote</h2></div>
<table width="600" height="20" border="0" align="center" cellspacing="0">
<tr><td align="center" width="107" class="plinky"><strong>Quantity:</strong></td>
<td class="plinky" align="center" width="85"><input name="prod_quantity" value="0" size="2" maxlength="4" class="quanw" type="text">
</td>
<td width="402" align="left" class="plinky"><strong>Enter Model Number:</strong>
<input name="model" style="width: 50px;" size="20" maxlength="20" class="quanw" value="" type="text">
</td>
</tr></table>
</form>
). You keep showing the status until the form is submitted.

