Tutorials | Application Example Php/mysql SHOPPING cart Program
?
if (! $session &&! $scid) {
$session = MD5 (Uniqid (rand ());
Setcookie ("SCID", "$session", Time () + 14400);
}/* Last number are 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);
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) DEFAULT ' 0.00 ' is 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)/* Heh, use whatever database name to put the 2 tables under in place of km Artshopper *
?>
/* Call functions like $cart->add_item and such, the Code. */
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.