PHP Operation to implement a multi-function shopping website, php multi-function shopping website
PHP operations to implement a multi-functional shopping website
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>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> </title>
<Script src = "bootstrap/js/jquery-1.11.2.min.js"> </script>
<Script src = "bootstrap/js/bootstrap. min. js"> </script>
<Link href = "bootstrap/css/bootstrap.min.css" rel = "stylesheet" type = "text/css"/>
</Head>
<Style>
. Title {
Margin-left: 750px;
Margin-top: 150px;
}
. Quanju {
Margin-left: pixel PX;
Margin-top:-460px;
}
. Name,. pwd {
Max-width: 120px;
}
. Yangshi1 {
Margin-top: 200px;
}
. Header {
Width: 100%;
Height: 80px;
Background: # e0e0e0;
}
. Ps {
Margin-left: 100px;
Margin-top:-100px;
}
</Style>
<Body>
<Form class = "form-horizontal" role = "form" action = "dengluchuli. php" method = "post">
<Div class = "header">
2. On the login processing page: dengluchuli. php
<? Php
Session_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>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> </title>
<Script src = "bootstrap/js/jquery-1.11.2.min.js"> </script>
<Script src = "bootstrap/js/bootstrap. min. js"> </script>
<Link href = "bootstrap/css/bootstrap.min.css" rel = "stylesheet" type = "text/css"/>
</Head>
<Body>
<H2> // 1. Find out how many items and total prices are in the shopping cart.
$ Uid = $ _ SESSION ["uid"];
If (empty ($ _ SESSION ["uid"]) {
Header ("location: loginpage. php ");
Exit;
}
Require_once "./DBDA. class. php ";
$ Db = new DBDA ();
// If there is a commodity in the shopping cart, retrieve the value
If (! Empty ($ _ SESSION ["gwd"]) {
$ Arr = $ _ SESSION ["gwd"];
$ Sum = 0;
$ Numbers = count ($ arr );
Foreach ($ arr as $ k =>$ v ){
// $ V [0]; // fruit name
// $ V [1]; // quantity purchased
$ SQL = "select * from fruit where ids = '{$ v [0]}'";
$ Attr = $ db-> query ($ SQL, 0 );
$ Dj = $ attr [0] [2]; // unit price
$ Sum = $ sum + $ dj * $ v [1]; // total price = unit price * quantity
}
}
Echo @ "<div style = 'margin-left: 250px '> The total number of items in the shopping cart is {$ numbers}, and the total price is: {$ sum} RMB </div> ";
?>
<A href = "loginpage. php"> // 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 the two-dimensional array.
// 2. If the item exists in the shopping cart, add 1 to the quantity
If (empty ($ _ SESSION ["gwd"]) {
// If it 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 to the quantity
Foreach ($ arr as $ k =>$ v ){
If ($ v [0] ==$ ids ){
$ 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>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> </title>
<Script src = "bootstrap/js/jquery-1.11.2.min.js"> </script>
<Script src = "bootstrap/js/bootstrap. min. js"> </script>
<Link href = "bootstrap/css/bootstrap.min.css" rel = "stylesheet" type = "text/css"/>
</Head>
<? Php
Session_start ();
$ Uid = $ _ SESSION ["uid"];
If (empty ($ _ SESSION ["uid"]) {
Header ("location: loginpage. php ");
Exit;
}
?>
<Body>
<H2>
6. Delete the goodsdel. php page.
<? Php
Session_start ();
$ Zj = $ _ GET ["zj"];
// If the fruit quantity 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
<? Php
Session_start ();
$ Ids = $ _ GET ["ids"];
// View the 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 is sufficient for purchase
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 "insufficient inventory! ";
Exit;
}
}
// Submit the order
// Deduct 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 ($ stjd );
// Add Order Details
Foreach ($ arr as $ v ){
$ Ddxq = "insert into orderdetails values ('', '{$ ddh}', '{$ v [0]}', '{$ v [1]}') ";
$ Axq = $ db-> query ($ ddxq );
}
} Else {
Echo "insufficient balance. Please recharge your account! ";
Exit;
}
Header ("location: shopping_list.php ");
The user account balance has been reduced: