Home  >  Article  >  Backend Development  >  Detailed explanation of graphic code for adding products to shopping cart in PHP

Detailed explanation of graphic code for adding products to shopping cart in PHP

黄舟
黄舟Original
2017-03-17 09:40:541971browse

This article mainly introduces the function of adding products to the shopping cart in PHP in detail. It has a certain reference value. Interested friends can refer to it.

Today I am practicing the shopping cart and submitting orders. I’m a little dizzy writing this, so let’s organize it by the way. This shopping cart is relatively simple. It is used for short-term storage and is not stored in the database. The shopping cart is simply familiar to people who love online shopping. I am writing this. Before entering the shopping cart, we must first conceive. We need to call up a table from the database. Here I use the fruit table, followed by the login table. I use the login table to adjust the user name and password. After everything is ready, there are three situations when you should consider putting it in the shopping cart:

The first situation: There is nothing in the shopping cart

Second case: There is already this product in the shopping cart. When adding it again, the quantity should be considered +1

Third case: There are products in the shopping cart, but there is no such product

The following picture is the database table used:

Detailed explanation of graphic code for adding products to shopping cart in PHPDetailed explanation of graphic code for adding products to shopping cart in PHP

The following is the code for the login page :



登录

用户名:


密 码:


After the login page is written, you need to enter the processing page and retrieve the user name and password from the database:


Query($sql);

if($arr[0][0]==$pwd && !empty($pwd)) //判断所填写的密码和取到的密码是一样的,而且密码不能为空
{
 $_SESSION["uid"]=$uid;
 header("location:main.php");
}
else
{
 echo"登录失败";
}

The login page is as shown in the figure:

Detailed explanation of graphic code for adding products to shopping cart in PHP

Now we need to enter the main page, and pull out all the fruit information from the database, and then we can add it to the shopping cart. This function.


大苹果购物网

Query($sql); foreach($arr as $v) { echo" // 从数据库调出我们所需要的内容 //这里的购买相当于添加购物车的功能 "; } ?> Query($sql1); foreach($danjia as $n) { $sum=$sum + $n[0]*$k[1]; } } echo"购物车有{$zhonglei}种商品,总价格为{$sum}元"; ?>
代号 水果名称 水果价格 原产地 货架 库存量
{$v[0]}{$v[1]} {$v[2]} {$v[3]} {$v[4]} {$v[5]} 购买

查看购物车 浏览商品 查看账户

The main page is as shown in the picture:

Detailed explanation of graphic code for adding products to shopping cart in PHP

The most important thing is to add the shopping cart page

In this way, you can display the shopping cart page. The shopping cart page code is as follows:

购物车中有以下商品:

Query($sql,1); foreach($att as $n) { echo"";} } ?>
商品名称 商品单价 购买数量
{$n[1]} {$n[2]} {$v[1]} 删除

查看购物车 浏览商品 查看账户

14 15

In this way, the shopping cart page will be displayed as shown in the figure:

Detailed explanation of graphic code for adding products to shopping cart in PHP

This is just a relatively simple process of adding to the shopping cart, but there are still many links in the middle that have not been perfected. For example, after adding to the shopping cart, the number of products in the database has been reduced, and operations such as deleting products in the shopping cart have not yet been done. Will be added later.

The above is the detailed content of Detailed explanation of graphic code for adding products to shopping cart in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn