PHP implements the simple Shopping Cart function (2), php shopping cart
In the previous article, I wrote a simple function to add to the shopping cart. After the products in the shopping cart are deleted and the orders are submitted, the inventory and the customer account balance are not complete,
This article continues to improve the previous article. The previous article was written to the function of deleting the shopping cart. To reduce the number of deleted code, we need
When the link is deleted, the value of ids is changed to $ arr index value $ k.
<A href = 'shanchu. php? Sy = {$ k} '> Delete </a> </td>
In this way, after entering the delete page, we can directly obtain the index value sy, which makes it much easier to delete the page.
The code for deleting the page is as follows: shanchu. php
1 <? Php 2 session_start (); 3 $ sy =$ _ GET ["sy"]; 4 5 // locate the data 6 $ arr =$ _ SESSION ["gwc"]; 7 $ arr [$ sy] based on the index; // The data to be deleted 8 9 // if the quantity is not 1, the quantity is reduced by 110 if ($ arr [$ sy] [1]> 1) 11 {12 $ arr [$ sy] [1] = $ arr [$ sy] [1]-1; 13} 14 else // if the number is 1, remove 15 {16 unset ($ arr [$ sy]); 17} 18 19 $ _ SESSION ["gwc"] = $ arr; // Save the content of the shopping cart 20 21 header ("location: gouwuche. php ");
In this way, you can delete the page. Here is the code for submitting the page, which is relatively cumbersome. When it comes to submitting the page, we will think of inventory and balance. We only need to submit the order, then the inventory will be reduced,
The relative balance will also be reduced. Of course, if the inventory is insufficient or the balance is insufficient, you should also prompt the purchaser
Two tables are created here to store data.
Add a tag at the end of gouwuche. php.
<div><a href="tijiaodd.php"></a><div>
The following is the code of tijiaodingdd. php.
1 <? Php 2 session_start (); 3 header ("Content-type: text/html; charset = UTF-8 "); // prevent garbled 4 $ uid =$ _ SESSION ["uid"]; 5 // first check the account balance 6 include ("DADB. class. php "); 7 $ db = new DADB (); 8 $ ysql =" select account from login where username = '{$ uid }'"; 9 $ yarr = $ db-> Query ($ ysql); 10 $ yarr [0] [0]; // total 11 12 13 // total price of the shopping cart, previously, we wrote 14 $ arr = array (); 16 17 if (! Empty ($ _ SESSION ["gwc"]) 18 {19 $ arr = $ _ SESSION ["gwc"]; 20} 21 $ sum = 0; 22 foreach ($ arr as $ v) 23 {24 $ v [1]; // number of products in the shopping cart: 25 $ psql = "select price from fruit WHERE ids = '{$ v [0]}'"; 26 $ parr = $ db-> Query ($ psql); 27 foreach ($ parr as $ k) 28 {29 $ k [0]; // The unit price of the product is 30 $ sum + = $ k [0] * $ v [1]; 31} 32} 33 34 // determine whether the balance meets the conditions for purchasing 35 if ($ yarr [0] [0]> = $ sum) 36 {// The balance is met, to determine inventory 37 foreach ($ arr as $ v) 38 {39 $ ksql = "select number from fruit where ids = '{$ v [0]} '"; 40 $ karr = $ db-> Query ($ ksql); 41 $ karr [0] [0]; // This is inventory 42 if ($ karr [0] [0] <$ v [1]) // It indicates inventory shortage, inform the customer of inventory shortage 43 {44 echo "inventory shortage"; 45 exit; 46} 47} 48 49 // after judgment, the order must be submitted 50 // the account balance is deducted 51 $ kcsql = "update login set account = account-{$ sum} where username =' {$ uid }'"; 52 $ db-> Query ($ kcsql, 0); // here the statement is modified, so add 053 54 // deduct inventory 55 foreach ($ arr as $ v) 56 {57 $ kcksql = "update fruit set number = number-$ v [1] where ids = '{$ v [0]}'"; 58 $ db-> Quer Y ($ kcksql, 0); 59} 60 61 // all work is done, at this time, we should submit the Order 62 // here I made two tables in the database, after adding the submitted order to the table, you can save 63 64 // Add Order 65 $ ddh = date ("YmdHis "); 66 $ time = date ("Y-m-d H: I: s"); 67 $ sdd = "insert into orders values ('{$ ddh }', '{$ uid}', '{$ time}') "; 68 $ db-> Query ($ sdd, 0 ); 69 70 // Add order details 71 foreach ($ arr as $ v) 72 {73 $ sddxq = "insert into orderdetails values ('', '{$ ddh }', '{$ v [0]}', '{$ v [1]}') "; 74 $ db-> Query ($ sddxq, 0 ); 75} 76} 77 78 els E79 {80 echo "insufficient balance"; 81 exit; 82} 83 84?>
The shopping cart is the same as the product in the previous shopping cart. Now let's look at the tables in the database.
Note: Because the zhangsan account has been used for many tests, the account price in the login table has changed to 33.6 yuan. The product price we put into the shopping cart is 16.8 yuan, so there is no problem here
Such a simple shopping cart can be completely completed.