Example of php shopping cart function implementation

Source: Internet
Author: User
Tags psql
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}
 
  Query ($ SQL); foreach ($ arr as $ v) {echo" 
   // Call out the required content from the database 
    // 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 ";?> 
  
Code Fruit name Fruit price Origin Shelf Inventory
{$ V [0]}{$ V [1]} {$ V [2]} {$ V [3]} {$ V [4]} {$ V [5]} Purchase

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:

  
   Query ($ SQL, 1); foreach ($ att as $ n) {echo" 
    ";}}?> 
   
Product Name Unit Price Quantity purchased
{$ 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!

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.