Detailed explanation of shopping website implementation examples using PHP

巴扎黑
Release: 2023-03-16 07:56:01
Original
15105 people have browsed it

The following editor will bring you a case of implementing a multi-functional shopping website in PHP. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. The page that needs to be implemented:

Index.aspx: Browse the product page and display the product list , users can click "Add to 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


        

果 蔬 网

用户登录

Copy after login

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 "登陆失败!"; }
Copy after login

This way you can contact the database. This is the login account of the database. and password, 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}元

"; ?> 登录 query($sql,0); foreach($arr as $v){ echo ""; } ?>
代号 名称 价格 产地 库存 操作
{$v[0]} {$v[1]} {$v[2]} {$v[3]} {$v[4]} 加入购物车
查看购物车
Copy after login

4. Then make the homepage processing page: 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; }
Copy after login

The effect is as shown:

5. Then view the shopping cart page, you can see the products in the shopping cart, the unit price and the total price: gouwuche.php


        

购物车清单

$v){ //$v[0];$v[1]; $sql = "select * from fruit where ids='{$v[0]}'"; $a = $db->query($sql,0); //var_dump($v[1]); echo ""; $dj = $a[0][2]; $sum = $sum+$dj*$v[1]; } } //echo "

购物车中商品总数为{$numbers}个,商品总价为:{$sum}元

"; ?>
代号 名称 价格 产地 购买数量 操作
{$v[0]} {$a[0][1]} {$a[0][2]} {$a[0][3]} {$v[1]} 删除
提交订单
Copy after login

The effect is as shown in the figure:

6. 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");
Copy after login

7.. Then make 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");
Copy after login
The user account balance has been reduced:


The above is the detailed content of Detailed explanation of shopping website implementation examples using PHP. For more information, please follow other related articles on the PHP Chinese website!

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!