PHP implementation of the product added to the shopping cart function of the graphic code detailed

Source: Internet
Author: User
This article is mainly for everyone to introduce the PHP implementation of product added shopping cart function, with a certain reference value, interested in small partners can refer to

Today in practice shopping cart and submit orders, write a little dizzy, by the way also tidy up, this shopping cart is relatively simple, for short-term storage, and not stored to the database, shopping cart for the love of people who buy online can not be familiar with, before writing shopping cart, we first need to conceive, We need to first pull out a table from the database, here I use the fruit table, followed by the login table, I use the login table, used to call the user name and password, all ready to consider into the shopping cart will have three kinds of situations:

In the first case, there's nothing in the shopping cart.

The second situation: the shopping cart inside already has this product, again joins this case to consider is the quantity to be +1

The third situation: there is a product in the shopping cart, but there is no such product

is the database table used:

Here is the code for the login page:


<body><form action= "chuli.php" method= "POST" > <p style= "margin-left:500px; margin-top:200px;  height:250px; width:250px; border:1px dashed Black ">  <p style=" margin-left:100px; ">

After the login page is written, you need to go to the processing page and bring up the user name and password from the database:


<?phpsession_start (); The opening session must be written to the first header ("Content-type:text/html;charset=utf-8"), $uid =$_post["UID"]; Get the username and password from the login 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 the password entered is the same as the password taken, And the password cannot be empty {$_session["UID"]= $uid; Header ("location:main.php");} else{echo "Login Failed";}

Login page:

Below to go to the main page, from the database of all the fruit information to be transferred out, and then we will be added to the shopping cart function.


Main Page:

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

<?phpsession_start (); $ids = $_get["IDs"];if (Empty ($_session["GWC"]) {//1. The shopping cart is empty, the first time you click add cart $arr = Array (  Array ($ids, 1)); $_session["GWC"]= $arr;} else{//Is not the first time to click//To determine if the item is in the cart $arr = $_session["GWC");//Save First $chuxian = false; foreach ($arr as $v) {  if ($v [0]== $ids  {   $chuxian = true;  }} if ($chuxian) {  //3. If the item is in the cart for  ($i =0; $i <count ($arr); $i + +)  {   if ($arr [$i][0]== $ids)   {    $arr [$i][1]+=1;   }  }  $_session["GWC"] = $arr; } else {  //2. If the item is not in the cart  $ASG = Array ($ids, 1);  $arr [] = $ASG;  $_session["GWC"] = $arr; }}header ("location:gouwuche.php");

This can be displayed to the shopping cart page, the shopping cart page code is as follows:

The following items are available in the 

This goes to the shopping cart page showing:

This is only relatively simple to add to the shopping cart, but there are many links in the middle is not perfect, for example, after adding to the shopping cart, the number of products in the database is reduced, the product in the shopping cart deleted and other operations have not been done, follow-up.

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.