Php adds a simple shopping cart, and php adds a shopping cart.

Source: Internet
Author: User

Php adds a simple shopping cart, and php adds a shopping cart.
This shopping cart is relatively simple. It is used for temporary storage and is not stored in the database. Shopping cart is almost familiar to online shoppers. before writing to the shopping cart, first, we need to think about it. First, we need to call up a table from the database. Here I use the fruit table, and secondly the login table. I use the login table, for user name and password adjustment, there are three situations to consider putting all the items in the shopping cart after they are ready:

First case: there is nothing in the shopping cart.

The second case: This product already exists in the shopping cart. If you add this product again, the quantity must be greater than 1.

Case 3: There is a product in the shopping cart, but there is no such product

Used database tables:

The following is the logon Page code:

<Body> <form action = "chuli. php" method = "post"> <div>

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:

1 <? Php 2 session_start (); // to enable the session, you must write the first line 3 header ("Content-type: text/html; charset = UTF-8 "); 4 5 $ uid = $ _ POST ["uid"]; // obtain the username and password 6 $ pwd = $ _ POST ["pwd"] From the logon page; 7 8 include ("DADB. class. php "); 9 $ db = new DADB (); 10 11 $ SQL =" select password from login where username = '{$ uid }'"; 12 $ arr = $ db-> Query ($ SQL); 13 14 if ($ arr [0] [0] = $ pwd &&! Empty ($ pwd) // determine whether the entered password is the same as the obtained password, the password cannot be blank. 15 {16 $ _ SESSION ["uid"] = $ uid; 17 header ("location: main. php "); 18} 19 else20 {21 echo" Logon Failed "; 22}

Logon page:

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.

1 

Home page:

The most important thing is to add a shopping cart page.

1 <? Php 2 session_start (); 3 4 $ ids = $ _ GET ["ids"]; 5 6 7 if (empty ($ _ SESSION ["gwc"]) 8 {9 // 1. the shopping cart is empty. For the first time, click Add cart 10 $ arr = array (11 array ($ ids, 1) 12); 13 $ _ SESSION ["gwc"] = $ arr; 14} 15 else16 {17 // not the first time you click 18 // determine whether the item exists in the shopping cart 19 $ arr =$ _ SESSION ["gwc"]; // first save 20 21 $ chuxian = false; 22 foreach ($ arr as $ v) 23 {24 if ($ v [0] = $ ids) 25 {26 $ chuxian = true; 27} 28} 29 30 if ($ chuxian) 31 {32 // 3. if the shopping cart contains this item 33 34 for ($ I = 0; $ I <count ($ arr); $ I ++) 35 {36 if ($ arr [$ I] [0] ==$ ids) 37 {38 $ arr [$ I] [1] + = 1; 39} 40} 41 42 $ _ SESSION ["gwc"] = $ arr; 43} 44 else45 {46 // 2. if the item 47 $ asg = array ($ ids, 1); 48 $ arr [] = $ asg; 49 $ _ SESSION ["gwc"] = $ arr; 50} 51 52} 53 header ("location: gouwuche. php ");

In this way, the shopping cart page is displayed. The code of the shopping cart page is as follows:

1 
1 <? Php 2 session_start (); 3 // $ uid = $ _ SESSION ["uid"]; 4 5 $ arr = array (); 6 7 if (! Empty ($ _ SESSION ["gwc"]) 8 {9 $ arr = $ _ SESSION ["gwc"]; 10} 11 include ("DADB. class. php "); 12 $ db = new DADB (); 13 14 foreach ($ arr as $ v) 15 {16 global $ db; 17 $ SQL = "select * from fruit where ids = '{$ v [0]}'"; 18 $ att = $ db-> Query ($ SQL, 1 ); 19 foreach ($ att as $ n) 20 {21 echo "<tr> 22 <td >{$ n [1] }</td> 23 <td >{$ n [2]} </td> 24 <td >{$ v [1] }</td> 25 <td> <a href = 'shanchu. php? Ids = {$ v [0]} '> Delete </a> </td> 26 </tr> ";} 27 28} 31?>

</Table>
10 <div>
11 <a href = "gouwuche. php"> View the shopping cart </a>

12 <a href = "main. php"> browse products </a>

13 <a href = "zhanghu. php"> View account </a> </div> 14 15 </body>

In this way, the shopping cart page is displayed:

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.