Shopping cart code implemented by cookie + mysql in php

Source: Internet
Author: User
Tags current time md5 mysql in rand setcookie

Shopping cart implementation is a necessary function during the development of e-commerce websites. This article introduces the implementation of php shopping cart code.

Principle: cookie stores the shopping cart ID and data stores the shopping cart data.

The code is as follows: Copy code

<? Php
// Code for generating the shopping cart session
If (! $ Session &&! $ Scid ){
/*
Session is used to differentiate each shopping cart, which is equivalent to the ID card number of each car;
Scid is only used to identify a shopping cart ID, which can be considered as the name of each vehicle;
When neither the id nor the session value of the shopping cart exists, a new shopping 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
*/
SetCookie (scid, $ session, time () + 14400 );
/*
Set the shopping cart cookie
Variable name: scid (I wonder if a $ number is missing here? = "Correction: add" "to the scid)
Variable value: $ session
Effective Time: current time + 14400 seconds (within 4 hours)
For detailed usage of the setcookie function, refer to the php Manual ~
*/
}

//----------------------------------------------------
// Start the shopping cart class
Class Cart {// start 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 whether the 'product' is included in the 'shopping cart' in the 'table'
That is, whether the product has been placed in the shopping cart
*/
$ Result = mysql_query ($ query );
If (! $ Result ){
Return 0;
  }
/*
Query failed
*/
$ NumRows = mysql_num_rows ($ result );
If ($ numRows = 0 ){
Return 0;
/*
If no value is found, 0 is returned.
*/
} Else {
$ Row = mysql_fetch_object ($ result );
Return $ row-> quantity;
/*
If yes, the number of items is returned.
Here it is necessary to explain the mysql_fetch_object function (which will be used below ):
[Mysql_fetch_object () is similar to mysql_fetch_array (). There is only one difference-returning an object instead of an array .]
The above sentence is taken from the php manual and should be quite clear ~
Simply put, to retrieve a field in a record, use "->" instead of 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 to first check whether this type of item has been placed in the car.
*/
If ($ qty = 0 ){
$ Query = "insert into $ table (session, product, quantity) VALUES ";
$ Query. = "('$ session', '$ product',' $ quantity ')";
Mysql_query ($ query );
/* If the vehicle does not exist, add the item to the vehicle */
} Else {
$ Quantity + = $ qty; // If yes, the quantity is increased based on the original quantity.
$ 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 an item (table name, session, item)
*/
$ Query = "delete from $ table WHERE session = '$ session' AND product =' $ product '";
Mysql_query ($ query );
/*
Delete this type of item in the shopping cart
*/
  }
Function modify_quantity ($ table, $ session, $ product, $ quantity ){
/*
Modify the number of items (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 ){
/*
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 items in the car first
*/
If (mysql_num_rows ($ result)> 0 ){
While ($ row = mysql_fetch_object ($ result )){
/*
If the number of items is greater than 0, the price is determined and calculated one by one.
*/
$ Query = "SELECT price FROM inventory WHERE product = '$ row-> product '";
$ InvResult = mysql_query ($ query );
/*
Search for the price of this item from the inventory table
*/
$ Row_price = mysql_fetch_object ($ invResult );
$ Total + = ($ row_price-> price * $ row-> quantity );
/*
Total price + = price of the item * quantity of the item
(You can see it clearly :))
*/
    }
   }
Return $ total; // return the total price.
  }
Function display_contents ($ table, $ session ){
/*
Get detailed information about all items in a car
*/
$ Count = 0;
/*
Item count
Note that this variable is used not only to count the number of items, but also as a subscript in the returned value array to distinguish each item!
*/
$ Query = "SELECT * FROM $ table WHERE session = '$ session' order by id ";
$ Result = mysql_query ($ query );
/*
First retrieve all items in the car
*/
While ($ row = mysql_fetch_object ($ result )){
/*
Obtain detailed information for each item
*/
$ Query = "SELECT * FROM inventory WHERE product = '$ row-> product '";
$ Result_inv = mysql_query ($ query );
/*
Search for 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;
/*
Add all the details about the item to the $ contents array.
$ Contents is a two-dimensional array
The first subscripts are used to differentiate different information (such as the item name, price, and quantity) of each item)
The second subscript distinguishes different items (this is the role of the $ count variable defined above)
*/
$ Count ++; // The number of items plus one (that is, the next item)
  }
$ Total = $ this-> cart_total ($ table, $ session );
$ Contents [final] = $ total;
/*
Call the cart_total function above to calculate the total price.
And put it in the $ contents array.
*/
Return $ contents;
/*
Returns the array
*/
 }
Function num_items ($ table, $ session ){
/*
Total number of returned item types (that is, two identical items are regarded as nonsense --!)
*/
$ Query = "SELECT * FROM $ table WHERE session = '$ session '";
$ Result = mysql_query ($ query );
$ Num_rows = mysql_num_rows ($ result );
Return $ num_rows;
/*
Retrieve all items in the car and obtain the number of database lines affected by this 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 to say, two identical items are counted as two items --#)
*/
$ Quant = 0; // total number of items
$ Query = "SELECT * FROM $ table WHERE session = '$ session '";
$ Result = mysql_query ($ query );
While ($ row = mysql_fetch_object ($ result )){
/*
Retrieve each item one by one
*/
$ Quant + = $ row-> quantity; // add the number of items to the total quantity.
  }
Return $ quant; // The total number of returned results.
 }
}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.