Php shopping cart instance

Source: Internet
Author: User
Here we provide a php shopping cart instance code, which is a development instance suitable for the development of the mall or shopping system reference. it tells you that if you add products to the shopping mall and delete them, and the database design instance of the shopping cart. inventory table, the code is as follows: create... here we provide a php shopping cart instance code, which is a development instance suitable for the development of the mall or shopping system reference. it tells you that if you add products to the shopping mall and delete them, and the database design instance of the shopping cart.

Inventory table, the code is as follows:

Create table inventory (product tinytext not null, quantity tinytext not null, id int (4) default '0' not null auto_increment, description tinytext not null, price float (10, 2) default '0. 00 'not null, category char (1) default ''not null, key id (id), primary key (id), key price (price )); insert into inventory values ('Hard disk ', '5', '1', '80 GB', '000000', '1'); insert into inventory values ('CPU ', '12', '2', 'p4-2.4g ', '123', '1'); insert into inventory values ('DVD-rom', '7 ', '3', '12x', '000000', '1'); insert into inventory values ('mainboard ', '3', '4', 'ASUS ', '123', '2'); insert into inventory values ('dashboard ', '6', '5', '64m', '123', '1 '); insert into inventory values ('recorders ', '4', '6', '52w', '123', '1 '); shoping table create table shopping (session tinytext not null, product tinytext not null, quantity tinytext not null, card tinytext not null, id int (4) default '0' not null auto_increment, key id (id), primary key (id); shoper table create database shopper; use shopper; create table shopping (session tinytext not null, product tinytext not null, quantity tinytext not null, card tinytext not null, id int (4) default '0' not null auto_increment, key id (id), primary key (id )); create table inventory (product tinytext not null, quantity tinytext not null, id int (4) default '0' not null auto_increment, description tinytext not null, price float (10, 2) default '0. 00 'not null, category char (1) default ''not null, key id (id), primary key (id), key price (price )); insert into inventory values ('Hard disk ', '5', '1', '80 GB', '000000', '1'); insert into inventory values ('CPU ', '12', '2', 'p4-2.4g ', '123', '1'); insert into inventory values ('DVD-rom', '7 ', '3', '12x', '000000', '1'); insert into inventory values ('mainboard ', '3', '4', 'ASUS ', '123', '2'); insert into inventory values ('dashboard ', '6', '5', '64m', '123', '1 '); insert into inventory values ('recorders ', '4', '6', '52w', '123', '1 ');

The main. php shopping page has the following code:

/* Mysql -- user = root -- password = your_password */

Include ("shoppingcart. php "); $ cart = new cart; $ table =" shopping ";/* query and display information in all inventory Tables */$ query =" select * from inventory "; $ invresult = mysql_query ($ query); if (! ($ Invresult) {echo "query failed
"; Exit;} echo" the following products are available for subscription: "; echo" "; Echo" "; Echo" "; While ($ row_inventory = mysql_fetch_object ($ invresult) {echo" "; Echo" "; Echo" "; Echo" "; Echo" "; Echo" ";} Echo"
Product No. Product Name Unit PriceRemaining quantity Product Description Add to shopping cart
". $ Row_inventory-> id ."". $ Row_inventory-> product ."". $ Row_inventory-> price ."". $ Row_inventory-> quantity ."". $ Row_inventory-> description ."Product. "'>
"; Echo"
Quantity of products in the shopping cart: ". $ cart-> quant_items ($ table, $ session); echo"

Empty shopping cart "; // shoppingcart. php code "; Exit ;}$ mysql_select = mysql_select_db (" shopper ", $ mysql_link); if (! ($ Mysql_select) {echo "failed to open database
"; Exit;}/* shopping cart class */class cart {function check_item ($ table, $ session, $ product) {$ query = "select * from $ table where session = '$ session' and product =' $ product'"; $ result = mysql_query ($ query); if (! $ Result) {return 0 ;}$ numrows = mysql_num_rows ($ result); if ($ numrows = 0) {return 0 ;} else {$ row = mysql_fetch_object ($ result); return $ row-> quantity;} function add_item ($ table, $ session, $ product, $ quantity) {$ qty = $ this-> check_item ($ table, $ session, $ product); if ($ qty = 0) {$ query = "insert into $ table (session, product, quantity) values "; $ query. = "('$ session', '$ product',' $ quantity ')"; mysql_query ($ query);} else {$ quantity + = $ qty; $ query = "update $ table set quantity = '$ quantity' where session = '$ session' and"; $ query. = "product = '$ product'"; mysql_query ($ query) ;}} function delete_item ($ table, $ session, $ product) {$ query = "delete from $ table where session = '$ session' and product =' $ product'"; mysql_query ($ query);} function modify_quantity ($ table, $ session, $ product, $ quantity) {$ query = "update $ table set quantity = '$ quantity' where session = '$ session'"; $ query. = "and product = '$ product'"; mysql_query ($ query);} function clear_cart ($ table, $ session) {$ query = "delete from $ table where session = '$ session'"; mysql_query ($ query);} function cart_total ($ table, $ session) {$ query = "select * from $ table where session = '$ session'"; $ result = mysql_query ($ query); if (mysql_num_rows ($ result)> 0) {while ($ row = mysql_fetch_object ($ result) {$ query = "select price from inventory where product = '$ row-> product '"; $ invresult = mysql_query ($ query); $ row_price = mysql_fetch_object ($ invresult); $ total + = ($ row_price-> price * $ row-> quantity );}} return $ total;} $ count ++;} echo""; $ Total = $ this-> cart_total ($ table, $ session); $ contents [" final "] = $ total; return $ contents;} function num_items ($ table, $ session) {$ query = "select * from $ table where session = '$ session'"; $ result = mysql_query ($ query); $ num_rows = mysql_num_rows ($ result ); return $ num_rows;} function quant_items ($ table, $ session) {$ quant = 0; $ query = "select * from $ table where session = '$ session '"; $ result = mysql_query ($ query); while ($ row = mysql_fetch_object ($ result) {$ quant + = $ row-> quantity;} return $ quant ;}} // add to the shopping carinclude ("shoppingcart. php "); $ cart = new cart; $ table =" shopping "; echo" your shopping list:
"; $ Cart-> add_item ($ table, $ session, $ product, '1'); $ cart-> display_contents ($ table, $ session); echo"
Total amount of your purchases: ". $ cart-> cart_total ($ table, $ session); echo"
"; // Clear the shopping carinclude (" shoppingcart. php "); $ cart = new cart; $ table =" shopping "; $ cart-> clear_cart ($ table, $ session); echo" number of products in the shopping cart ". $ cart-> num_items ($ table, $ session); echo "";


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.