Code for the PHP shopping cart
Published by: [Longlong16] Time: [2006-11-1]
if (! $session &&! $scid) {
$session = MD5 (Uniqid (rand ()));
Setcookie (SCID, $session, Time () + 14400);
}/* Last number was expiration time in seconds, 14400 sec = 4 hrs */
Class Cart {
function Check_item ($table, $session, $product) {
$query = SELECT * from $table WHERE session= ' $session ' and product= ' $product ';
$result = mysql_query ($query);
if (! $result) {
return 0;
}
$numRows = mysql_num_rows ($result);
if ($numRows = = 0) {
return 0;
} else {
$row = Mysql_fetch_object ($result);
return $row->quantity;
}
}
function Add_item ($table, $session, $product, $quantity) {
$qty = $this->check_item ($table, $session, $product);
if ($qty = = 0) {
$query = INSERT into $table (session, product, quantity) VALUES;
$query. = (' $session ', ' $product ', ' $quantity ');
mysql_query ($query);
} else {
$quantity + = $qty;
$query = UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session ' and;
$query. = product= ' $product ';
mysql_query ($query);
}
}
function Delete_item ($table, $session, $product) {
$query = DELETE from $table WHERE session= ' $session ' and product= ' $product ';
mysql_query ($query);
}
function modify_quantity ($table, $session, $product, $quantity) {
$query = UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session ';
$query. = and product= ' $product ';
mysql_query ($query);
}
function Clear_cart ($table, $session) {
$query = DELETE from $table WHERE session= ' $session ';
mysql_query ($query);
}
function Cart_total ($table, $session) {
$query = SELECT * from $table WHERE session= ' $session ';
$result = mysql_query ($query);
if (mysql_num_rows ($result) > 0) {
while ($row = Mysql_fetch_object ($result)) {
$query = SELECT Price from inventory WHERE product= ' $row->product ';
$invResult = mysql_query ($query);
$row _price = mysql_fetch_object ($invResult);
$total + = ($row _price->price * $row->quantity);
}
}
return $total;
}
function display_contents ($table, $session) {
$count = 0;
$query = SELECT * from $table the WHERE session= ' $session ' ORDER by ID;
$result = mysql_query ($query);
while ($row = Mysql_fetch_object ($result)) {
$query = SELECT * from inventory WHERE product= ' $row->product ';
$result _INV = mysql_query ($query);
$row _inventory = mysql_fetch_object ($result _inv);
$contents [product][$count] = $row _inventory->product;
$contents [price][$count] = $row _inventory->price;
$contents [quantity][$count] = $row->quantity;
$contents [total][$count] = ($row _inventory->price * $row->quantity);
$contents [description][$count] = $row _inventory->description;
$count + +;
}
$total = $this->cart_total ($table, $session);
$contents [Final] = $total;
return $contents;
}
function Num_items ($table, $session) {
$query = SELECT * from $table WHERE session= ' $session ';
$result = mysql_query ($query);
$num _rows = mysql_num_rows ($result);
return $num _rows;
}
function Quant_items ($table, $session) {
$quant = 0;
$query = SELECT * from $table WHERE session= ' $session ';
$result = mysql_query ($query);
while ($row = Mysql_fetch_object ($result)) {
$quant + = $row->quantity;
}
return $quant;
}
}
?>
/*
This section contains a description of how to create the tables on your MySQL server.
# MySQL Dump 6.0
#
# Host:localhost Database:kmartshopper
#--------------------------------------------------------
# Server Version 3.22.25
#
# Table structure for table ' inventory '
#
CREATE TABLE Inventory (
Product Tinytext not NULL,
Quantity Tinytext not NULL,
ID int (4) DEFAULT ' 0 ' not NULL auto_increment,
Description Tinytext not NULL,
Price Float (10,2) The DEFAULT ' 0.00 ' not NULL,
Category char (1) DEFAULT ' not NULL,
KEY ID (ID),
PRIMARY KEY (ID),
KEY Price
);
#
# Table structure for table ' shopping '
#
CREATE TABLE Shopping (
Session Tinytext not NULL,
Product Tinytext not NULL,
Quantity Tinytext not NULL,
Card Tinytext not NULL,
ID int (4) DEFAULT ' 0 ' not NULL auto_increment,
KEY ID (ID),
PRIMARY KEY (ID)
);
*/
Example
Include (shoppingcart.php);
$cart = new Cart;
$mysql _link = mysql_connect (localhost, wwwrun,);
$mysql _select_db (Kmartshopper, $mysql _link) 2 tables under in place of Kmartshopper
The next can user the function of class to do something
This was easy
?>
http://www.bkjia.com/PHPjc/631862.html www.bkjia.com true http://www.bkjia.com/PHPjc/631862.html techarticle PHP Shopping Cart code publisher: [LONGLONG16] Time: [2006-11-1]? if (! $session! $scid) {$session =md5 (uniqid (rand ())); Setcookie (SCID, $session, Time () +14400); }/*lastnumberisexpira ...