Home > Backend Development > PHP Tutorial > 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

烟雨青岚
Release: 2023-04-09 07:58:02
forward
2447 people have browsed it

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:

<?php
include ("conn.php");
$sql="select * from produce";//查询所有商品
$rs=mysql_query($sql,$conn);//执行sql语句,得到一个结果集
while($row=mysql_fetch_array($rs))//遍历结果集
{
?>
<table width="343" height="152" border="1"style="float:left">
  <tr>
   <td width="124"rowspan="3"><img
src="images/<?php echo$row["pimg"]?>" width="123" height="121" border="0"/></td>
   <td width="203"height="35">货物名称:<?php echo$row["pname"]?></td>
  </tr>
  <tr>
   <tdheight="28">货物价格:<?php echo$row["price"]?></td>
  </tr>
  <tr>
   <td height="27"align="center"><ahref="buy.php?id=<?php echo $row["pid"]?>&pname=<?php
echo $row["pname"]?>">购买</a></td>
 </tr> </table>
<?php
}
?>
Copy after login

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

<?php
 $conn=mysql_connect("localhost","root","");
 mysql_select_db("shop",$conn);
 mysql_query("set names gb2312");
?>
Copy after login

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:

<?php
session_start();//使用session之前一定要将session开启
ob_start();//要清空缓存就必须ob_start()
$pid=$_GET["id"];//得到购买物品的id
$name=$_GET["pname"];//得到购买物品的名字
$arr=$_SESSION["mycar"];//将session中的变量取出来
//下面先判断这个变量是否是数组,可以得到以前是否买过东西
if(is_array($arr))
{
//如果是数组,说明以前买过东西
//如果买过东西又分两种情况:
     if(array_key_exists($pid,$arr))
     {
     //1、array_key_exists($pid,$arr)判断$arr中是否存在键值为$pid的一个一维数组,如果存在的话,就说明此商品以前购买过,只需要把数量加1
          $uu=$arr[$pid];
 //从二维数组里拿出对应的一维数组,该一维数组包括id name num 三个值
          $uu["num"]=$uu["num"]+1;  //改变数量,将数量加1
          $arr[$pid]=$uu;
 //改完后再将此一维数组放回二维数组中
     }
     else
     {   //2.此商品第一次购买,就将得到的id和name值组成一个一维数组
          $arr[$pid]=array("pid"=>$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)
?>
Copy after login

The following is the code of the shopping cart:

<?php
session_start();//启用session
$arr=$_SESSION["mycar"];//从session中拿出二维数组
?>
//下面将数组里的数据即客户所购买的物品展示出来
<table width="600" height="37"border="1">
  <tr>
   <tdwidth="96">商品ID</td>
   <tdwidth="158">商品名称</td>
   <tdwidth="154">商品数量</td>
   <tdwidth="177">删除</td>
  </tr>
<?php
foreach($arr as $a)//遍历这个二维数组
{
?>
     <tr>
     <td width="96"><?phpecho $a["pid"]?></td>//物品的id
   <td width="158"><?phpecho $a["name"]?></td>//物品的名称
   <td width="154"><?phpecho $a["num"]?></td>//物品的数量
   <td width="177"><ahref="delete.php?id=<?php echo $a[pid]?>">删除</a></td>//点击删除超链接到”delete.php”,将物品的id传过去
</tr>
<?php
}
?>
</table>
</form>
<ahref="index.php">返回继续购物</a>//返回到首页
Copy after login

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:

<?php
session_start();//启动session
ob_start();//清空缓存必须启动的项
$pid=$_GET["id"];//得到通过get方式传过来的id
$arr=$_SESSION["mycar"];//拿出session里的二维数组
foreach($arr as$key=>$proId)//遍历该二维数组中的键值,这里也就是商品的id
{
     if($key==$pid)//判断键值等于传过来的商品id
     {
          unset($arr[$key]);//清除该一维数组
     }
}
$_SESSION["mycar"]=$arr;//将清除之后的二维数组重新放到session里
ob_clean();//清除缓存
header("location:car.php");//跳转到购物车
?>
Copy after login

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template