PHP implementation of a multi-function shopping website case, multi-function shopping website case

Source: Internet
Author: User

PHP implementation of a multi-function shopping website case, multi-function shopping website case

1. pages to be implemented:

Index. aspx: browse the product page and display the product list. You can click "add to shopping cart".

ViewCart. aspx: view the shopping cart page and display the purchased item information. You can click "Delete" and "Submit add order purchase ".

ViewAccount. aspx: view your account balance

Login. aspx: logon page

Ii. functions:

1. display the product list

2. Implement the purchase function. The quantity and total price of items in the shopping cart are dynamically displayed during purchase.

3. Click to view the shopping cart to display the purchased items. Pay attention to the "quantity purchased" column. If you click to buy a product multiple times, the "quantity purchased" column will increase.

4. Delete the purchased items in the shopping cart.

If the "quantity purchased" of a product is 1, click "delete" to delete the product from the shopping cart;

If the "quantity purchased" is greater than 1, click "delete" to reduce the number of purchased items by 1. When the number of purchased items is 1, click Delete to delete the item.

5. After viewing the shopping cart, you can click "Browse products" to continue purchasing. The quantity and total price of purchased items are displayed on the page.

6. After "view shopping cart", you can submit the order.

However, when submitting an order, you must complete the following functions:

(A) check whether the user has logged on. If you have not logged on, go to the Login. aspx page.

(B) Check whether the user's account balance is sufficient for this purchase

(C) Check whether the inventory quantity is sufficient for this purchase

(D) if all the preceding conditions are met

I. deduct the total price of this purchase from the user account

Ii. deduct the quantity of each item from the item inventory

Iii. Add the purchased item information to the order table and order content table

7. Click View account to view the account balance of the user.

The operation code is as follows:

1. First, create a login page: loginpage. php

<! DOCTYPE html> 

Effect

2. On the login processing page: dengluchuli. php

<? Phpsession_start (); $ uid = $ _ POST ["uid"]; $ pwd = $ _ POST ["pwd"]; require_once ". /DBDA. class. php "; $ db = new DBDA (); $ SQL =" select * from login where username = '{$ uid }'"; $ arr = $ db-> query ($ SQL, 0); if ($ arr [0] [2] = $ pwd &&! Empty ($ pwd) {$ _ SESSION ["uid"] = $ uid; header ("location: shopping_list.php");} else {echo "Login Failed! ";}

In this way, you can contact the database. This is the login account and password of the database, verify the account and password, and then jump to the home page: shopping_list.php

3. Now the homepage page is shopping_list.php.

<! DOCTYPE html> 

4. Perform the processing page of the home page: shoppingchuli. php

<? Phpsession_start (); // obtain the passed primary key value and add it to the SESSION of the shopping cart $ ids =$ _ GET ["ids"]; // if this is the first time you add a shopping cart, create a two-dimensional array and store it in the SESSION. // if it is not added for the first time, there are two cases. // 1. if this item does not exist in the shopping cart, create a one-dimensional array and drop it into two-dimensional space. // 2. if this item exists in the shopping cart, add 1if (empty ($ _ SESSION ["gwd"]) {// if this is the first time you add a shopping cart, create a two-dimensional array and store it in the SESSION $ arr = array ($ ids, 1); $ _ SESSION ["gwd"] = $ arr ;} else {$ arr = $ _ SESSION ["gwd"]; if (deep_in_array ($ ids, $ arr) {// if the item exists in the shopping cart, add 1 foreach ($ arr as $ k => $ v) {if ($ v [0] = $ I Ds) {$ arr [$ k] [1] ++ ;}}$ _ SESSION ["gwd"] = $ arr ;} else {// If This item does not exist in the shopping cart, create a one-dimensional array and drop it into the two-dimensional array $ arr =$ _ SESSION ["gwd"]; $ attr = array ($ ids, 1); $ arr [] = $ attr; $ _ SESSION ["gwd"] = $ arr;} header ("location: shopping_list.php"); function deep_in_array ($ value, $ array) {foreach ($ array as $ item) {if (! Is_array ($ item) {if ($ item = $ value) {return true;} else {continue ;}} if (in_array ($ value, $ item )) {return true;} else if (deep_in_array ($ value, $ item) {return true ;}} return false ;}

Effect

5. Check the shopping cart page again to see the items and unit prices and total prices in the shopping cart: gouwuche. php

<! DOCTYPE html> 

Effect

6. Delete the goodsdel. php page.

<? Phpsession_start (); $ zj = $ _ GET ["zj"]; // if the number of fruits is greater than 1, minus 1 // if the fruit quantity is equal to 1, remove $ arr =$ _ SESSION ["gwd"]; if ($ arr [$ zj] [1]> 1) {$ arr [$ zj] [1] = $ arr [$ zj] [1]-1;} else {unset ($ arr [$ zj]); // clear the array $ arr = array_values ($ arr); // re-index the array} $ _ SESSION ["gwd"] = $ arr; header ("location: add_list.php ");

7 .. then submit the page: tijiao. php

<? Phpsession_start (); $ ids = $ _ GET ["ids"]; // view balance $ uid = $ _ SESSION ["uid"]; require_once ". /DBDA. class. php "; $ db = new DBDA (); $ SQL =" select account from login where username = '{$ uid }'"; $ arr = $ db-> query ($ SQL, 0); $ aye = $ arr [0] [0]; // balance // var_dump ($ aye ); if (! Empty ($ _ SESSION ["gwd"]) {$ arr = $ _ SESSION ["gwd"]; $ sum = 0; // $ numbers = count ($ arr); foreach ($ arr as $ v) {$ SQL = "select * from fruit where ids = '{$ v [0]}'"; $ price = $ db-> query ($ SQL, 0 ); $ dj = $ price [0] [2]; $ sum = $ sum + $ dj * $ v [1] ;}} else {echo "You have not purchased any products yet! "; // Header (" shopping_list.php "); exit;} // determine whether the balance meets the conditions for purchasing if ($ aye >=$ sum) {// determine the inventory foreach ($ arr as $ v) {$ skc = "select name, numbers from fruit where ids = '{$ v [0]}'"; $ akc = $ db-> query ($ SQL, 0); var_dump ($ akc); $ kc = $ akc [0] [4]; // inventory // var_dump ($ kc); if ($ kc <$ v [1]) {echo "inventory is insufficient! "; Exit ;}} // submit the order // deduct the balance from your account $ skye = "update login set account = account-{$ sum} where username = '{$ uid }'"; $ zhye = $ db-> query ($ skye); // deduct inventory foreach ($ arr as $ v) {$ skckc = "update fruit set numbers = numbers-{$ v [1]} where ids = '{$ v [0]}'"; $ sykc = $ db-> query ($ skckc);} // Add order $ ddh = date ("Y-m-d H: I: s "); $ time = time (); $ stjd = "insert into orders values ('{$ time}', '{$ uid}', '{$ ddh }')"; $ wcdh = $ db-> query ($ st Jd); // Add order details foreach ($ arr as $ v) {$ ddxq = "insert into orderdetails values ('', '{$ ddh }', '{$ v [0]}', '{$ v [1]}') "; $ axq = $ db-> query ($ ddxq );}} else {echo "balance is insufficient. Please recharge your account! "; Exit;} header (" location: shopping_list.php ");

The user account balance has been reduced:


The above PHP implementation of a multi-functional shopping website is all the content shared by the small editor. I hope to give you a reference and support for the house of friends.

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.