Php to add the product to the shopping cart function (1), php shopping cart
Today, I am practicing shopping cart and submitting orders. I am a little dizzy. By the way, I also sorted out that this shopping cart is relatively simple and used for temporary storage and is not stored in the database, shopping cart is almost familiar to people who love online shopping. before writing a shopping cart, we must first think about it. We need to call out a table from the database first, here I use the fruit table, followed by the login table, and I use the login table to call the user name and password, when everything is ready, you have to consider the following three situations:
First case:Nothing in the shopping cart
Case 2: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 style =" margin-left: 500px; margin-top: 200px; height: 250px; width: 250px; border: 1px dashed black "> <div style =" margin-left: 100px; ">
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:
<? 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 ";}
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.
<H2> Big Apple Shopping Network
Home page:
The most important thing is to add a 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 ");
In this way, the shopping cart page is displayed. The code of the shopping cart page is as follows:
<H2> the shopping cart contains the following items:
In this way, the shopping cart page is displayed:
This is just a simple addition to the shopping cart, but there are still many incomplete links in the middle. For example, after adding the shopping cart to the shopping cart, operations such as reducing the number of products in the database and deleting the products in the shopping cart have not been done yet, make up later.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.