How to implement a simple shopping cart in php

藏色散人
Release: 2023-03-06 18:18:02
Original
3528 people have browsed it

How to implement a simple shopping cart in php: first use session to record a two-dimensional array; then store the session in the array; and finally add, delete, and modify the array.

How to implement a simple shopping cart in php

Recommended: "PHP Video Tutorial"

This article introduces a shopping cart code implemented in php, with complete functions , has certain reference value

Here we provide you with a simple php shopping cart code, from adding shopping products to making purchases, in the mall development, this function is indispensable

The specific analysis is as follows:

The operations on the items in the shopping cart generally include the following: adding items, deleting items, and submitting orders;

The essence of the method is: store the session in the array, Add, delete, and modify the array. Each set of records in the array is information about a product (number, price, etc.);

The idea of solving the shopping cart is to use session to record a two-dimensional array . One dimension represents each product, and the two dimensions include the product ID, the quantity of the product, etc. You can add it by yourself. Anyway, it is two-dimensional. You can add as many product attributes as you want.

The operation process of the shopping cart: First, log in to the website to browse the products; then, purchase the specified products and enter the shopping cart page. On this page, you can change the quantity of the products, delete the products, clear the shopping cart, Continue shopping and so on; finally, generate orders, submit orders and other operations.

Please see the powerful comments for details;

Log in first;

 

登入页面

帐号:

密码:

Copy after login

Picture:

Login processing:

Query($sql); if(!empty($zhang)&&!empty($mi)&&$mi = $arr){ $_SESSION["zhang"] = $zhang; header("location:zym.php"); } else { echo "登入失败"; } ?>
Copy after login

A very simple login, nothing to say;

After he logged in, he went directly to the main page:

 

长腿璇购物商城

Copy after login
Query($sql); foreach ($arr as $v) { echo " 
"; } ?> Query($sql1); foreach($danjia as $n) { $aa=$aa + $n[0]*$k[1]; } } echo"数量:{$zhonglei}
价格:{$aa}元"; ?>
Copy after login
代号 水果名称 水果价格 水果产地 水果库存 操作
{$v[0]} {$v[1]} {$v[2]} {$v[3]} {$v[4]} 加入购物车
查看账户 查看购物车
Copy after login

Pictures coming:

##When I click Add to Cart:

The quantity and price above have changed, indicating that it has been added to the shopping cart Inside the car;

Let’s see how it is handled (powerful comments):

Copy after login

Next, make the shopping cart page:

 

查看购物车

Copy after login
query($sql); foreach ($att as $a) { echo "
"; // 蔬果的名称 // 单价 // 取int数量 // 这个地方也可以加索引shanchu.php?sy={$v} } } ?>
Copy after login
商品名称 商品单价 商品数量 操作
{$a[1]} {$a[2]} {$v[1]} 删除
提交订单
Copy after login

Above picture:

##You can see the number of big apples It is 4. If I click delete, the condition is that there is a big apple and the number is greater than one. Clicking delete will reduce the number by one:

The number of white grapes is 1. If I click Delete, the condition is that the number is not greater than one, so that it can be removed from the array;

Having said this, let’s take a look at the delete page:

$v) { if($v[0]==$ids) { if($v[1]>1){ //要删除的数据 $arr[$key][1]-=1; } else{ //数量为1的情况下,移除该数组 unset($arr[$key]); } } } $_SESSION["gwc"] = $arr; //记得扔到session里面 header("location:ckgwc.php"); //删除完跳转回去
Copy after login

High energy! !

Submit order page, I will only come to a processing page. If you need, you can fill in the link yourself:

query($sye); $ye[0][0];//这是余额 $ann=array(); if(!empty($_SESSION["gwc"])) { $ann=$_SESSION["gwc"]; } $zhonglei = count($ann); $aa=0;//总价格 foreach($ann as $k) { $k[0];//水果代号 $k[1];//水果数量 $sql1="select jiage from sgbiao where ids='{$k[0]}'"; $danjia=$db->Query($sql1); foreach($danjia as $n) { $aa=$aa + $n[0]*$k[1]; } } //判断余额是否满足 if($ye[0][0]>=$aa) { //钱够,判断库存 foreach($ann as $v) { $skc = "select sgname,kucun from sgbiao WHERE ids='{$v[0]}'"; //水果代号$v[0] $akc = $db->query($skc); $akc[0][1];//库存 //比较是否满足库存 if($akc[0][1]<$v[1]) { echo "{$akc[0][0]}库存不足"; //退出 exit; } } //提交订单: //i. 从用户账户中扣除本次购买的总价格 //ii. 从商品库存中扣除本次每种商品的购买数量 //iii. 向订单表和订单内容表中加入本次购买的商品信息 //扣除账户余额 $skcye = "update yonghu set zhanghu = zhanghu-{$aa} WHERE zhang = '{$zhang}'"; $db->query($skcye,0); //扣除库存 foreach($ann as $v) { $skckc = "update sgbiao set kucun = kucun-{$v[1]} WHERE ids='{$v[0]}'"; //水果代号$v[0] $db->query($skckc,0); } //添加订单信息 //取当前时间 $time = time(); //自动生成订单号 $ddh = date("YmdHis"); $sdd = "insert into dingdan VALUES ('{$ddh}','$zhang','$time')"; $db->query($sdd,0); //添加订单内容 foreach ($ann as $v) { $sddxq = "insert into ddneirong VALUES ('','$ddh','{$v[0]}','{$v[1]}')"; $db->query($sddxq,0); } } else { echo "钱不够"; exit; }
Copy after login
Like this Come on, most of the functions of the shopping cart have been implemented;

Let’s take a look at the effect after clicking to submit the order:

1. Reduce the fruit Inventory:

2. Add order:

3. Add order content:

4. Deduct the purchaser’s Account balance:

The above is the contents of the shopping cart

The above is the detailed content of How to implement a simple shopping cart in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!