This article explains in detail how to use php to implement the shopping cart function and explains it in the form of diagrams + code. First, there are several simple login pages
After logging on to the page, you need to go to the processing page and call up the user name and password from the database:
Query ($ SQL); if ($ arr [0] [0] ==$ pwd &&! Empty ($ pwd) // Determine whether the entered password is the same as the obtained password, and the password cannot be blank {$ _ SESSION ["uid"] = $ uid; header ("location: main. php ");} else {echo" logon failed ";}
The logon page is displayed.
Next we will go to the home page, and call out all the fruit information from the database. then we will implement the function of adding to the shopping cart.
Big Apple Shopping Network
{$ Zhonglei}{$ Sum}
Code |
Fruit name |
Fruit price |
Origin |
Shelf |
Inventory |
|
Query ($ SQL); foreach ($ arr as $ v) {echo"
{$ V [0]} | // Call out the required content from the database
{$ V [1]} |
{$ V [2]} |
{$ V [3]} |
{$ V [4]} |
{$ V [5]} |
Purchase | // The purchase here is equivalent to the function of adding a shopping cart.
";}?> Query ($ sql1); foreach ($ danjia as $ n) {$ sum = $ sum + $ n [0] * $ k [1] ;}} echo "shopping cart hasType of goods, the total price isYuan ";?>
View shopping cart browse product View Account
Home page display
Next is the add shopping cart page
Then the main shopping cart interface is displayed as follows:
The shopping cart contains the following items:
Product Name |
Unit Price |
Quantity purchased |
|
Query ($ SQL, 1); foreach ($ att as $ n) {echo"
{$ N [1]} |
{$ N [2]} |
{$ V [1]} |
Delete |
";}}?>
View shopping cart browse product View Account
14 15
Then we go to the delete page, and process when there is only one item in the shopping cart and one item in the same way.
1) {$ arr [$ sy] [1] = $ arr [$ sy] [1]-1;} else // if the number is 1, remove {unset ($ arr [$ sy]);} $ _ SESSION ["gwc"] = $ arr; // save the content header ("location: gouwuche. php ");
As for the submission page, we need to consider the balance, inventory and other factors, so it is complicated,
I'm not afraid of it. go to the code.
Query ($ ysql); $ yarr [0] [0]; // Total amount // The total price of the shopping cart. $ arr = array (); if (! Empty ($ _ SESSION ["gwc"]) {$ arr = $ _ SESSION ["gwc"] ;}$ sum = 0; foreach ($ arr as $ v) {$ v [1]; // quantity of products in the shopping cart $ psql = "select price from fruit WHERE ids = '{$ v [0]}'"; $ parr = $ db-> Query ($ psql); foreach ($ parr as $ k) {$ k [0]; // product unit price $ sum + = $ k [0] * $ v [1] ;}} // Determine whether the balance meets the purchase if ($ yarr [0] [0]> = $ sum) {// if the balance is met, you need to determine the inventory foreach ($ arr as $ v) {$ ksql = "select number from fruit where ids = '{$ v [0]}'"; $ karr = $ db-> Query ($ ksql ); $ karr [0] [0]; // This is an inventory if ($ Karr [0] [0] <$ v [1]) // indicates insufficient inventory. in this case, inform the customer of insufficient inventory {echo "insufficient inventory"; exit ;}} // after judgment, the order must be submitted. // the account balance is deducted from $ kcsql = "update login set account = account-{$ sum} where username = '{$ uid }'"; $ db-> Query ($ kcsql, 0); // Here is the modify statement, so add 0 // deduct inventory foreach ($ arr as $ v) {$ kcksql = "update fruit set number = number-$ v [1] where ids = '{$ v [0]}'"; $ db-> Query ($ kcksql, 0);} // when all the work is done, we should submit the order. // Here I have made two tables in the database, add the submitted order to the table and save it. // add the order $ ddh = date ("Ymd His "); $ time = date (" Y-m-d H: I: s "); $ sdd =" insert into orders values ('{$ ddh }', '{$ uid}', '{$ time}') "; $ db-> Query ($ sdd, 0 ); // add order details foreach ($ arr as $ v) {$ sddxq = "insert into orderdetails values ('', '{$ ddh }', '{$ v [0]}', '{$ v [1]}') "; $ db-> Query ($ sddxq, 0 );}} else {echo "insufficient balance"; exit ;}?>
There is no problem with implementing the function
The above is a detailed description of the php shopping cart function example. For more information, see other related articles in the first PHP community!