PHP simple shopping cart code sharing and analysis-PHP source code

Source: Internet
Author: User
PHP simple shopping cart code sharing and analysis

1.[PHP] code
// Code generated by 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 used to identify only one ID number of the shopping cart and can be considered as the name of each car; when neither the id nor the session value of the shopping cart exists, a new shopping cart */$ session = md5 (uniqid (rand () is generated ())); /* generate a unique shopping cart session number rand () first generate a random number, and then generate a unique string based on the random number, finally, perform md5 */SetCookie (scid, $ session, time () + 14400) on the string;/* Set the variable name of the cookie in the shopping cart: scid (I wonder if a $ number is missing here ?) Variable value: $ session Effective Time: current time + 14400 seconds (within 4 hours) detailed usage of the setcookie function, please refer to the php Manual ~ */} Class Cart {// Start function check_item ($ table, $ session, $ product) of the shopping Cart {/* check items (table name, session, item) */$ query = SELECT * FROM $ table WHERE session = '$ session' AND product =' $ product '; /* check whether the 'product' is included in the 'cart' in the 'table', that is, whether 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, 0 */} else {$ row = mysql_fetch_object ($ result); return $ row-> quantity;/* if found, returns the number of items. 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 using the subscript */} 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 first check whether this type of item has been put INTO the car */if ($ qty = 0) {$ query = insert into $ table (session, product, quantity) VALUES; $ query. = ('$ session', '$ product',' $ quantity '); mysql_query ($ query);/* add this item to the vehicle if it is not in the vehicle */} else {$ Quantity + = $ qty; // If yes, add the quantity $ query = UPDATE $ table SET quantity = '$ quantity' WHERE session = '$ session' AND; $ query. = product = '$ product'; mysql_query ($ query);/* and modify 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_qu Antity ($ 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, $ s Ession) {/* total price of items in the car */$ query = SELECT * FROM $ table WHERE session = '$ session'; $ result = mysql_query ($ query ); /* First retrieve all items in the car */if (mysql_num_rows ($ result)> 0) {while ($ row = mysql_fetch_object ($ result )) {/* if there are more than 0 items, judge the price one by one and calculate */$ query = SELECT price FROM inventory WHERE product = '$ row-> product '; $ invResult = mysql_query ($ query);/* query the price of this item from the inventory (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 the car */$ count = 0;/* Note that this variable is not only used to count the number of items, more importantly, it will be used as the subscript in the returned value array to differentiate every 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 (inventory) table */$ row_inventory = mysql_fetch_object ($ result_inv); $ contents [product] [$ count] = $ ro W_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 subscript is to distinguish the different information of each item (such as the item name, price, quantity, and so on) the second subscript is to distinguish different items (this is the role of the $ count variable defined above) */$ count ++; // add one (The next item)} $ Tal = $ this-> cart_total ($ table, $ session); $ contents [final] = $ total;/* call the above cart_total function at the same time, calculate the total price and put it into the $ contents array */return $ contents;/* return the array */} function num_items ($ table, $ session) {/* Total number of returned item categories (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 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 counted as two items--#) */$ quant = 0; // total 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; // return the total quantity }}

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.