PHP shopping cart usage-update the number of shopping cart

Source: Internet
Author: User
The number of shopping carts means that we can delete the items at any time when we buy things, so that our shopping cart must update the records. next I will introduce you to the PHP shopping cart to update the shopping cart quantity program and the reasons, for more information, see. the code in the form section is as follows: l... the number of shopping carts means that we can delete the items at any time when we buy things, so that our shopping cart must update the records. next I will introduce you to the PHP shopping cart to update the shopping cart quantity program and the reasons, for more information, see.

The code is as follows:

 

The PHP processing code is as follows:

 $ Value) {$ sqlgx = "update 'cartemp 'set suliang =' $ value' where username = '". $ username. "'and flag = 0 and sp_id = '". $ key. "'"; mysql_query ($ sqlgx, $ conn); echo "script location. href = 'shopcat. php 'script ";} break; case" null ": $ null_ SQL =" delete from 'cartemp 'where username =' $ username' and flag = 0 "; // open source code phprm.com mysql_query ($ null_ SQL, $ conn); echo "script location. href = 'shopcat. php 'scr Ipt "; break; case" del ": $ id = $ _ GET ['id']; $ del_ SQL =" delete from 'cartemp 'where id = $ id "; mysql_query ($ del_ SQL, $ conn); echo "script location. href = 'shopcat. php 'script '; break;}?>

All of the above operations are performed using the database. The following is a complete class and the code is as follows:

 Quantity;/* if found, return the number of items. here it is necessary to explain the mysql_fetch_object function (which will be used below): [mysql_fetch_object () and mysql_fetch_array () are similar, there is only one difference-an object instead of an array is returned.] 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 = INSERTINTO $ table (session, product, quantity) VALUES; $ query. = ('$ session', '$ product',' $ quantity '); mysql_query ($ query);/* if no, like adding this item to a car */} else {$ Quantity + = $ qty; // If yes, add the quantity on the original base $ query = UPDATE $ tableSETquantity = '$ quantity 'wheresession =' $ 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 = DELETEFROM $ tableWHEREsession = '$ 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 $ tableSETquantity = '$ quantity' WHEREsession = '$ 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 = DELETEFROM $ tableWHEREsession = '$ session'; mysql_query ($ query);} function cart_total ($ table, $ Session) {/* total price of items in the car */$ query = SELECT * FROM $ tableWHEREsession = '$ 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 = SELECTpriceFROMinventoryWHEREproduct = '$ row-> product'; $ invResult = mysql_query ($ query ); /* search for 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 $ tableWHEREsession = '$ session' ORDERBYid; $ 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 * FROMinventoryWHEREproduct = '$ 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] = $ row_ I Nventory-> 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 distinguishes different items (this is the function of the $ count variable defined above) */$ count ++; // The number of items plus one (that is, the next item)} $ tot Al = $ 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 $ tableWHEREsession = '$ 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 $ tableWHEREsession = '$ 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 }}


Tutorial address:

Reprinted! But please include the article address ^

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.