<?php
The production code of the shopping cart session
if (! $session &&! $scid) {
/*
The session is used to distinguish each shopping cart, the equivalent of each car's identification number;
SCID is only used to identify a shopping cart ID number, can be regarded as the name of each car;
When both the ID and session value of the cart are not present, a new shopping cart is created
*/
$session = MD5 (Uniqid (rand ()));
/*
Generate a unique shopping cart session number
RAND () generates a random number first, uniqid () then produces a unique string based on the random number, and finally md5 the string.
*/
Setcookie (SCID, $session, Time () + 14400);
/*
Set up this shopping cart cookie
Variable name: SCID (Do you know if there is a $ number missing here?) = "Correction: SCID to add" ")
Variable Value: $session
Valid time: Current time + 14,400 seconds (within 4 hours)
For the detailed usage of the Setcookie function, let's see the PHP manual.
*/
}
//----------------------------------------------------
Start Shopping Cart Category
Class Cart {//start shopping cart class
function Check_item ($table, $session, $product) {
/*
Inspection items (table name, session, item)
*/
$query = "SELECT * from $table WHERE session= ' $session ' and product= ' $product '";
/*
Take a look at the ' product ' in the ' shopping cart ' in ' table '
That is, the product has been put into the shopping cart
*/
$result = mysql_query ($query);
if (! $result) {
return 0;
}
/*
Query failed
*/
$numRows = mysql_num_rows ($result);
if ($numRows = = 0) {
return 0;
/*
If not found, returns 0
*/
} else {
$row = Mysql_fetch_object ($result);
return $row->quantity;
/*
If found, returns the number of items
It is necessary to explain the Mysql_fetch_object function (which is also used below):
"Mysql_fetch_object () and mysql_fetch_array () are similar, with only a little difference--return an object instead of an array. 】
The above sentence from the PHP manual, it should be clear that the bar ~
Simply put, a field in a record should be labeled with "--" instead of as an array
*/
}
}
function Add_item ($table, $session, $product, $quantity) {
/*
Add new items (table name, session, item, quantity)
*/
$qty = $this->check_item ($table, $session, $product);
/*
Call the above function, first check if the item has been put in the car
*/
if ($qty = = 0) {
$query = "INSERT into $table (session, product, quantity) VALUES";
$query. = "(' $session ', ' $product ', ' $quantity ')";
mysql_query ($query);
/* If not in the car, add the item in the car */
} else {
$quantity + = $qty; If there is, increase the quantity on the original basis
$query = "UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session ' and";
$query. = "product= ' $product '";
mysql_query ($query);
/*
and modify the database
*/
}
}
function Delete_item ($table, $session, $product) {
/*
Delete items (table name, session, item)
*/
$query = "DELETE from $table WHERE session= ' $session ' and product= ' $product '";
mysql_query ($query);
/*
Remove this item from the Cart
*/
}
function modify_quantity ($table, $session, $product, $quantity) {
/*
Number of items modified (table name, session, item, quantity)
*/
$query = "UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session '";
$query. = "and product= ' $product '";
mysql_query ($query);
/*
Change the number of items to the value in the parameter
*/
}
function Clear_cart ($table, $session) {
/*
Empty the cart (nothing to say)
*/
$query = "DELETE from $table WHERE session= ' $session '";
mysql_query ($query);
}
function Cart_total ($table, $session) {
/*
Total items in the car
*/
$query = "SELECT * from $table WHERE session= ' $session '";
$result = mysql_query ($query);
/*
Take out all the items in the car first
*/
if (mysql_num_rows ($result) > 0) {
while ($row = Mysql_fetch_object ($result)) {
/*
If the number of items is >0, the price is judged and calculated
*/
$query = "Select Price from inventory WHERE product= ' $row->product '";
$invResult = mysql_query ($query);
/*
Find the price of this item from the Inventory (inventory) table
*/
$row _price = mysql_fetch_object ($invResult);
$total + = ($row _price->price * $row->quantity);
/*
Total Price + = The Item value * The number of items
(You should be able to see it:) )
*/
}
}
return $total; Return Total Price
}
function display_contents ($table, $session) {
/*
Get more information about all the items in the car
*/
$count = 0;
/*
Count of items
Note that this variable is not just for counting the number of items, but more importantly, it will be used as a subscript in the array of return values to differentiate each item!
*/
$query = "SELECT * from $table the WHERE session= ' $session ' ORDER by id ';
$result = mysql_query ($query);
/*
Take all the items out of the car first
*/
while ($row = Mysql_fetch_object ($result)) {
/*
Take details of each item separately
*/
$query = "SELECT * FROM inventory WHERE product= ' $row->product '";
$result _INV = mysql_query ($query);
/*
Find information about this item from the Inventory (inventory) table
*/
$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;
/*
Put all the details about the item into the $contents array
$contents is a two-dimensional array
The first set of subscripts is the difference between each item's different information (such as item name, price, quantity, etc.)
The second set of subscripts is the difference between different items (this is the role of the previously defined $count variables)
*/
$count + +; Number of items plus one (i.e. the next item)
}
$total = $this->cart_total ($table, $session);
$contents [Final] = $total;
/*
At the same time call the above cart_total function, calculate the total price
and put it in the $contents array
*/
return $contents;
/*
Returns the array to the
*/
}
function Num_items ($table, $session) {
/*
Returns the total number of item types (that is, two of the same thing counts as a nonsense--!)
*/
$query = "SELECT * from $table WHERE session= ' $session '";
$result = mysql_query ($query);
$num _rows = mysql_num_rows ($result);
return $num _rows;
/*
Remove all items in the car to get the number of database rows affected by the operation, i.e. the total number of items (nothing to say)
*/
}
function Quant_items ($table, $session) {
/*
Returns the total number of items (that is, two identical items are counted as two objects--#)
*/
$quant = Total amount of 0;//items
$query = "SELECT * from $table WHERE session= ' $session '";
$result = mysql_query ($query);
while ($row = Mysql_fetch_object ($result)) {
/*
Take each item out individually
*/
$quant + = $row->quantity; The amount of the item is added to the total amount.
}
return $quant; Return Total
}
}
Cookie+mysql implementation of the shopping cart code in PHP