Home>Article>Backend Development> PHP implements a multi-functional shopping website
This article mainly introduces how to use PHP to implement a multi-functional shopping website. Interested friends can refer to it. I hope it will be helpful to everyone.
1. Pages that need to be implemented:
Index.aspx: Browse the product page, display the product list, the user can click "Add to shopping cart" ".
ViewCart.aspx: View the shopping cart page, which displays purchased product information. You can click "Delete" and "Submit to add an order to purchase" the product
ViewAccount.aspx: View personal account balance
Login.aspx: Login page
2. Implementation functions:
1. Display product list
2. Implement the purchase function, and dynamically display the quantity of goods in the shopping cart and the total price of the goods when purchasing
3. After clicking to view the shopping cart, the purchased goods will be displayed. Pay attention to the "Purchase Quantity" column. If you click to buy a product multiple times, its "Purchase Quantity" will continue to increase.
4. Delete the purchased items in the shopping cart.
If the "Purchase Quantity" of a product is 1, when you click "Delete", the product will be deleted directly from the shopping cart;
If the "Purchase Quantity" of the product is greater than 1 , when you click "Delete" once, the purchase quantity will be reduced by 1. When the purchase quantity of the product reaches 1, and then click Delete, the product will be deleted
5. After viewing the shopping cart, you can also click "Browse Products" to continue purchasing. And the quantity of goods purchased and the total price are displayed above.
6. After "View Shopping Cart", you can submit the order.
But when submitting an order, the following functions must be completed:
(a) Check whether the user is logged in. If not logged in, go to the Login.aspx page
(b) Check whether the user account balance is sufficient for this purchase
(c) Check whether the inventory quantity is sufficient for this purchase
(d) If all the above conditions are met
i. Deduct the total price of this purchase from the user account
ii. Deduct the purchase quantity of each product from the product inventory
iii. Add it to the order table and order content table Add the product information of this purchase
7. Click View Account to view the user’s account balance
The operation code is as follows:
1. First make a login page: loginpage.php
The effect is as shown:
2. Create a login processing page: dengluchuli.php
query($sql,0); if($arr[0][2]==$pwd && !empty($pwd)){ $_SESSION["uid"]=$uid; header("location:shopping_list.php"); }else{ echo "登陆失败!"; }
This way you can contact the database. This is the login account and password for the database. Verify the account and password, and then jump to the homepage: shopping_list.php
3. Now make the homepage page: shopping_list. php
水果列表
$v){ //$v[0];//水果名称 //$v[1];//购买数量 $sql = "select * from fruit where ids='{$v[0]}'"; $attr = $db->query($sql,0); $dj = $attr[0][2]; //单价 $sum = $sum+$dj*$v[1]; //总价=单价*数量 } } echo @"购物车中商品总数为{$numbers}个,商品总价为:{$sum}元
"; ?> 登录
代号 | 名称 | 价格 | 产地 | 库存 | 操作 |
---|---|---|---|---|---|
{$v[0]} | {$v[1]} | {$v[2]} | {$v[3]} | {$v[4]} | 加入购物车 |
4. Then do the processing page of the homepage: shoppingchuli.php
$v){ if($v[0]==$ids){ $arr[$k][1]++; } } $_SESSION["gwd"]=$arr; }else{ //如果该商品购物车里面不存在,造一个一维数组扔到二维里面 $arr=$_SESSION["gwd"]; $attr=array($ids,1); $arr[]=$attr; $_SESSION["gwd"]=$arr; } } header("location:shopping_list.php"); function deep_in_array($value, $array) { foreach($array as $item) { if(!is_array($item)) { if ($item == $value) { return true; } else { continue; } } if(in_array($value, $item)) { return true; } else if(deep_in_array($value, $item)) { return true; } } return false; }
The effect is as shown:
5. Then check the shopping cart page and you can see the shopping cart The products, unit price and total price in: gouwuche.php
购物车清单
代号 | 名称 | 价格 | 产地 | 购买数量 | 操作 |
---|---|---|---|---|---|
{$v[0]} | {$a[0][1]} | {$a[0][2]} | {$a[0][3]} | {$v[1]} | 删除 |
The effect is as shown:
6. Then do the deletion processing page goodsdel.php
1){ $arr[$zj][1]=$arr[$zj][1]-1; }else{ unset($arr[$zj]); //清除数组 $arr=array_values($arr); //重新索引数组 } $_SESSION["gwd"] = $arr; header("location:add_list.php");
7. Then do the submission page: tijiao.php
##
query($sql,0); $aye = $arr[0][0];//余额 //var_dump($aye); if(!empty($_SESSION["gwd"])){ $arr = $_SESSION["gwd"]; $sum = 0; //$numbers = count($arr); foreach($arr as $v){ $sql = "select * from fruit where ids='{$v[0]}'"; $price = $db->query($sql,0); $dj = $price[0][2]; $sum = $sum+$dj*$v[1]; } }else{ echo "您还未购买商品!"; //header("shopping_list.php"); exit; } //判断余额是否满足购买 if($aye>=$sum){ //判断库存 foreach($arr as $v){ $skc = "select name,numbers from fruit where ids='{$v[0]}'"; $akc = $db->query($sql,0); var_dump($akc); $kc = $akc[0][4];//库存 //var_dump($kc); if($kc<$v[1]){ echo "库存不足!"; exit; } } //提交订单 //账户扣除余额 $skye = "update login set account=account-{$sum} where username='{$uid}'"; $zhye = $db->query($skye); //扣除库存 foreach($arr as $v){ $skckc = "update fruit set numbers=numbers-{$v[1]} where ids='{$v[0]}'"; $sykc = $db->query($skckc); } //添加订单 $ddh = date("Y-m-d H:i:s"); $time = time(); $stjd = "insert into orders values('{$time}','{$uid}','{$ddh}')"; $wcdh = $db->query($stjd); //添加订单详情 foreach($arr as $v){ $ddxq = "insert into orderdetails values('','{$ddh}','{$v[0]}','{$v[1]}')"; $axq = $db->query($ddxq); } }else{ echo "余额不足,请充值!"; exit; } header("location:shopping_list.php");The user account balance has been reduced:
PHP implements shopping website
Detailed explanation of examples of PHP implementing shopping website
php code example of how to implement a multi-functional shopping website
##
The above is the detailed content of PHP implements a multi-functional shopping website. For more information, please follow other related articles on the PHP Chinese website!