if(! $session && ! $scid) {
/*
session is used to distinguish each shopping cart, which is equivalent to the ID number of each car;
Scid is only used to identify a shopping cart ID number, which can be regarded as the name of each car;
When neither the id nor the session value of the cart exists, a new cart is generated
*/
$session = md5(uniqid(rand()));
/*
Generate a unique shopping cart session number
rand() first generates a random number, uniqid() then generates a unique string based on the random number, and finally md5 the string
*/
SetCookie(scid, $session, time() + 14400);
/*
Set this shopping cart cookie
Variable name: scid (I wonder if there is one missing $ number here? =》 Correction: Scid needs to add "")
Variable value: $session
Valid time: Current time +14400 seconds (within 4 hours)
For detailed usage of the setcookie function, please refer to the php manual~
*/
}
class Cart { //Start the shopping cart class
function check_item( $table, $session, $product) {
/*
Check items (table name, session, item)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
Check out if there is any 'product' in the 'shopping cart' in the 'table'
That is, has the product 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, return 0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
If found, the item quantity will be returned
It is necessary to explain the mysql_fetch_object function here (it will be used below):
【mysql_fetch_object() is similar to mysql_fetch_array(), with only one difference - returning an object instead of an array. 】
The above sentence is excerpted from the php manual, and you should be very clear~
Simply put, if you take a field in a record, you should use "->" instead of using subscript like 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 and check whether the item of this type has been put into the car
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*If there is no in the car, add the item like the car*/
} else {
$quantity += $qty; //If there is, increase the number 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);
/*
Delete such items in the shopping cart
*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
Modify the quantity of items (table name, session, item, quantity)
*/
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;
$query .= AND product=' $product' ;
mysql_query( $query);
/*
Modify the item quantity to the value in the parameter
*/
}
function clear_cart( $table, $session) {
/*
Clear the shopping cart (nothing to say)
*/
$query = DELETE FROM $table WHERE session=' $session' ;
mysql_query( $query);
}
function cart_total( $table, $session) {
/*
Total price of 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, then the price will be judged one by one and calculated
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
Find the price of the item from the inventory table
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
Total price += Price of this item * Quantity of this item
(Everyone should be able to see it clearly)
*/
}
}
return $total; //Return the total price
}
function display_contents( $table, $session) {
/*
Get detailed information about all items in the car
*/
$count = 0;
/*
Item count
Note that this variable is not only for counting the number of items, but more importantly, it will be used as a subscript in the return value array to distinguish each item!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
Take out all the items in the car first
*/
while( $row = mysql_fetch_object( $result)) {
/*
Get detailed information for each item separately
*/
$query = SELECT * FROM inventory WHERE product=' $row->product' ;
$result_inv = mysql_query( $query);
/*
Find information about this item from the 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 to distinguish the information of each item (such as item name, price, quantity, etc.)
The second set of subscripts is to distinguish different items (this is the function of the $count variable defined earlier)
*/
$count++; //Add one item quantity (i.e. the next item)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
At the same time, call the cart_total function above to calculate the total price
and put it in the $contents array
*/
return $contents;
/*
Return the array
*/
}
function num_items( $table, $session) {
/*
Returns the total number of items types (that is, two identical things are considered one, it seems nonsense - -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
Take out all items in the car and get the number of database rows affected by the operation, that is, the total number of items (nothing to say)
*/
}
function quant_items( $table, $session) {
/*
Returns the total number of all items (that is, two identical items are also counted as two items - -#)
*/
$quant = 0;// Total item quantity
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
Take out each item one by one
*/
$quant += $row->quantity; //Add the quantity of this item to the total quantity
}
return $quant; //Return the total amount
}
}
/*
session is used to distinguish each shopping cart, which is equivalent to the ID number of each car;
Scid is only used to identify a shopping cart ID number, which can be regarded as the name of each car;
When neither the id nor the session value of the cart exists, a new cart is generated
*/
$session = md5(uniqid(rand()));
/*
Generate a unique shopping cart session number
rand() first generates a random number, uniqid() then generates a unique string based on the random number, and finally md5 the string
*/
SetCookie(scid, $session, time() + 14400);
/*
Set this shopping cart cookie
Variable name: scid (I wonder if there is one missing $ number here? =》 Correction: Scid needs to add "")
Variable value: $session
Valid time: Current time +14400 seconds (within 4 hours)
For detailed usage of the setcookie function, please refer to the php manual~
*/
}
class Cart { //Start the shopping cart class
function check_item( $table, $session, $product) {
/*
Check items (table name, session, item)
*/
$query = SELECT * FROM $table WHERE session=' $session' AND product=' $product' ;
/*
Check out if there is any 'product' in the 'shopping cart' in the 'table'
That is, has the product 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, return 0
*/
} else {
$row = mysql_fetch_object( $result);
return $row->quantity;
/*
If found, the item quantity will be returned
It is necessary to explain the mysql_fetch_object function here (it will be used below):
【mysql_fetch_object() is similar to mysql_fetch_array(), with only one difference - returning an object instead of an array. 】
The above sentence is excerpted from the php manual, and you should be very clear~
Simply put, if you take a field in a record, you should use "->" instead of using subscript like 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 and check whether the item of this type has been put into the car
*/
if( $qty == 0) {
$query = INSERT INTO $table (session, product, quantity) VALUES ;
$query .= (' $session', ' $product', ' $quantity') ;
mysql_query( $query);
/*If there is no in the car, add the item like the car*/
} else {
$quantity += $qty; //If there is, increase the number 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);
/*
Delete such items in the shopping cart
*/
}
function modify_quantity( $table, $session, $product, $quantity) {
/*
Modify the quantity of items (table name, session, item, quantity)
*/
$query = UPDATE $table SET quantity=' $quantity' WHERE session=' $session' ;
$query .= AND product=' $product' ;
mysql_query( $query);
/*
Modify the item quantity to the value in the parameter
*/
}
function clear_cart( $table, $session) {
/*
Clear the shopping cart (nothing to say)
*/
$query = DELETE FROM $table WHERE session=' $session' ;
mysql_query( $query);
}
function cart_total( $table, $session) {
/*
Total price of 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, then the price will be judged one by one and calculated
*/
$query = SELECT price FROM inventory WHERE product=' $row->product' ;
$invResult = mysql_query( $query);
/*
Find the price of the item from the inventory table
*/
$row_price = mysql_fetch_object( $invResult);
$total += ( $row_price->price * $row->quantity);
/*
Total price += Price of this item * Quantity of this item
(Everyone should be able to see it clearly)
*/
}
}
return $total; //Return the total price
}
function display_contents( $table, $session) {
/*
Get detailed information about all items in the car
*/
$count = 0;
/*
Item count
Note that this variable is not only for counting the number of items, but more importantly, it will be used as a subscript in the return value array to distinguish each item!
*/
$query = SELECT * FROM $table WHERE session=' $session' ORDER BY id ;
$result = mysql_query( $query);
/*
Take out all the items in the car first
*/
while( $row = mysql_fetch_object( $result)) {
/*
Get detailed information for each item separately
*/
$query = SELECT * FROM inventory WHERE product=' $row->product' ;
$result_inv = mysql_query( $query);
/*
Find information about this item from the 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 to distinguish the information of each item (such as item name, price, quantity, etc.)
The second set of subscripts is to distinguish different items (this is the function of the $count variable defined earlier)
*/
$count++; //Add one item quantity (i.e. the next item)
}
$total = $this->cart_total( $table, $session);
$contents[final] = $total;
/*
At the same time, call the cart_total function above to calculate the total price
and put it in the $contents array
*/
return $contents;
/*
Return the array
*/
}
function num_items( $table, $session) {
/*
Returns the total number of items types (that is, two identical things are considered one, it seems nonsense - -!)
*/
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
$num_rows = mysql_num_rows( $result);
return $num_rows;
/*
Take out all items in the car and get the number of database rows affected by the operation, that is, the total number of items (nothing to say)
*/
}
function quant_items( $table, $session) {
/*
Returns the total number of all items (that is, two identical items are also counted as two items - -#)
*/
$quant = 0;// Total item quantity
$query = SELECT * FROM $table WHERE session=' $session' ;
$result = mysql_query( $query);
while( $row = mysql_fetch_object( $result)) {
/*
Take out each item one by one
*/
$quant += $row->quantity; //Add the quantity of this item to the total quantity
}
return $quant; //Return the total amount
}
}