Home>Article>Backend Development> Learn to use php to build a website shopping cart in two hours

Learn to use php to build a website shopping cart in two hours

烟雨青岚
烟雨青岚 forward
2020-07-14 13:26:50 2338browse

Learn to use php to build a website shopping cart in two hours

Online shopping has become fashionable. Customers select an item, put it in the shopping cart, and then return to continue shopping or go to the checkout. How to implement this function? Today capucivar will use PHP to implement this shopping cart function.

First, make a simple homepage, query several products from the database, display them on the homepage, and add a purchase button. The specific code is as follows:

 货物价格: 
" width="123" height="121" border="0"/> 货物名称:
&pname=">购买

By the way, I forgot to write conn.php, it is used to connect to the database:

Click the hyperlink to purchase Then go to buy.php,On this page, you need to store the purchased items. We can put the purchased items into a one-dimensional array, and then put suoyou's one-dimensional array into a two-dimensional array. dimensional array, and finally put the two-dimensional array into the session.

No matter how you modify the purchased items in the future, you can take them out of the session and modify them. This makes management very convenient. The specific code is as follows:

$pid,"name"=>$name,"num"=>1); } } else { $arr[$pid]=array("pid"=>$pid,"name"=>$name,"num"=>1); } $_SESSION["mycar"]=$arr;//购买完后,将此数组重新放入session中,便可以在各个页面看到此session ob_clean();//清空缓存 header("location:car.php");//跳转到购物车界面(car.php) ?>

The following is the code of the shopping cart:

 //下面将数组里的数据即客户所购买的物品展示出来 商品ID 商品名称 商品数量 删除 //物品的id //物品的名称 //物品的数量 //点击删除超链接到”delete.php”,将物品的id传过去 
">删除
返回继续购物//返回到首页

When deleting a product, first get the id of the product to be deleted. After getting the id, Take out the one-dimensional array corresponding to the obtained id in the two-dimensional array, clear the one-dimensional array (unset()), and then put the two-dimensional array back into session().Write the deletion code below:

$proId)//遍历该二维数组中的键值,这里也就是商品的id { if($key==$pid)//判断键值等于传过来的商品id { unset($arr[$key]);//清除该一维数组 } } $_SESSION["mycar"]=$arr;//将清除之后的二维数组重新放到session里 ob_clean();//清除缓存 header("location:car.php");//跳转到购物车 ?>

After writing the code, delete the product with ID 2 that has been purchased on capucivar

The shopping cart function is relatively simple, just Purchase and deletion are implemented. In fact, the shopping cart is relatively simple. As long as the idea is clear, it is as simple as calculating 1 1.

The function of the shopping cart is implemented as follows: purchase the product to get the id and name of the product, add these two values plus a quantity (1) and put them into a one-dimensional array. One product is A one-dimensional array, so many items naturally require a two-dimensional array.

Before this, check whether the product has been purchased before. If so, add one to the previous quantity. Otherwise, re-create a one-dimensional array and put the one-dimensional array into into a two-dimensional array and finally into the session.

When deleting, get the ID of the product to be deleted, then find the one-dimensional array that stores the product from the two-dimensional array, clear the one-dimensional array, and then put the two-dimensional array into the session. In this way, a simple shopping cart function similar to the one above is implemented.

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/qq1355541448/article/details/26371777

Recommended tutorial: "php tutorial"

The above is the detailed content of Learn to use php to build a website shopping cart in two hours. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete