Php implements the shopping cart function. Take the Big Apple shopping network as an example. The code above..., shopping cart Shopping Network

Source: Internet
Author: User
Tags psql

Php implements the shopping cart function. Take the Big Apple shopping network as an example.

First, there are several simple login pages

<Body> <form action = "chuli. php" method = "post"> <div> <? Phpsession_start (); // when the session is enabled, the header must be written to the first line ("Content-type: text/html; charset = UTF-8 "); $ uid = $ _ POST ["uid"]; // obtain the username and password from the logon page $ pwd = $ _ POST ["pwd"]; include ("DADB. class. php "); $ db = new DADB (); $ SQL =" select password from login where username = '{$ uid }'"; $ arr = $ db-> 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.

<H2> Big Apple Shopping Network 

Home page display

Next is the Add shopping cart page

<? Phpsession_start (); $ ids = $ _ GET ["ids"]; if (empty ($ _ SESSION ["gwc"]) {// 1. the shopping cart is empty. For the first time, click Add cart $ arr = array ($ ids, 1); $ _ SESSION ["gwc"] = $ arr ;} else {// This is not the first time you click. // you can check whether this item exists in the shopping cart $ arr =$ _ SESSION ["gwc"]; // save $ chuxian = false first; foreach ($ arr as $ v) {if ($ v [0] ==$ ids) {$ chuxian = true ;}} if ($ chuxian) {// 3. for ($ I = 0; $ I <count ($ arr); $ I ++) {if ($ arr [$ I] [0] ==$ ids) {$ arr [$ I] [1] + = 1 ;}} $ _ SESSION ["gwc"] = $ arr;} else {// 2. $ asg = array ($ ids, 1); $ arr [] = $ asg; $ _ SESSION ["gwc"] = $ arr ;}} header ("location: gouwuche. php ");

Then the main shopping cart interface is displayed as follows:

<H2> the shopping cart contains the following items: 
<A href = 'shanchu. php? Sy = {$ k} '> Delete </a> </td>
</Tr> ";}}?> </Table> <div> <a href = "gouwuche. php "> View the shopping cart </a> <a href =" main. php "> browse products </a> <a href =" zhanghu. php "> View account </a> </div> 14 15 </body>

  

 

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.

<? Phpsession_start (); $ sy =$ _ GET ["sy"]; // locate the data based on the index $ arr =$ _ SESSION ["gwc"]; $ arr [$ sy]; // The data to be deleted // if the quantity is not 1, the quantity is reduced by 1if ($ arr [$ sy] [1]> 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.

<? Php session_start (); header ("Content-type: text/html; charset = UTF-8"); // prevents garbled characters $ uid = $ _ SESSION ["uid"]; // first check the account balance include ("DADB. class. php "); $ db = new DADB (); $ ysql =" select account from login where username = '{$ uid }'"; $ yarr = $ db-> 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

 

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.