Home>Article>Backend Development> Implement simple php shopping cart code
Here we provide you with a simple php shopping cart code, from adding shopping products to making purchases. In 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 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.
Related learning recommendations:PHP programming from entry to proficiency
Please see the powerful comments for details;
Log in first;
登入页面
Picture:
##Login processing:Query($sql); if(!empty($zhang)&&!empty($mi)&&$mi = $arr){ $_SESSION["zhang"] = $zhang; header("location:zym.php"); } else { echo "登入失败"; } ?>A very simple login, nothing to say; After he logged in, he went directly to the main page:
长腿璇购物商城
代号 | 水果名称 | 水果价格 | 水果产地 | 水果库存 | 操作 |
{$v[0]} | {$v[1]} | {$v[2]} | {$v[3]} | {$v[4]} | 加入购物车 |
查看购物车
商品名称 | 商品单价 | 商品数量 | 操作 |
{$a[1]} | {$a[2]} | {$v[1]} | 删除 |
$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"); //删除完跳转回去
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; }In this way, shopping Most of the functions of the cart have been implemented; Let’s take a look at the effect after clicking to submit the order: 1. Reduce 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 Implement simple php shopping cart code. For more information, please follow other related articles on the PHP Chinese website!