Home>Article>Backend Development> How to implement shopping cart function in PHP session

How to implement shopping cart function in PHP session

coldplay.xixi
coldplay.xixi forward
2020-07-17 17:17:54 2744browse

How to implement shopping cart function in PHP session

In the wamp environment, use PHP session session control to complete the shopping cart effect. The data is stored in an array for practice. There is no connection to the database. The effect is good and simple and easy to understand. The following are each Part of the code

common.php

1,'img'=>'img/1.jpg','title'=>'小米移动电源5000mAh','price'=>49), array('id'=>2,'img'=>'img/2.jpg','title'=>'20000mAh小米移动电源2','price'=>149), array('id'=>3,'img'=>'img/3.jpg','title'=>'小米圈铁耳机Pro','price'=>129), array('id'=>4,'img'=>'img/4.jpg','title'=>'小米家电动滑板车','price'=>1999), array('id'=>5,'img'=>'img/5.jpg','title'=>'小米笔记本','price'=>3499), array('id'=>6,'img'=>'img/6.jpg','title'=>'米家LED智能台灯','price'=>169), array('id'=>7,'img'=>'img/7.jpg','title'=>'小米体重秤','price'=>99), array('id'=>8,'img'=>'img/8.png','title'=>'小米电视3s 48英寸','price'=>2599) );

index.php

     商品展示  

我的购物车

action.php

 $value) { if($id == $value['id']){ $arrData = $arrPro[$key]; } } //用一个新的二维数组把商品信息存起来 $arrDatax[0][$arrData['id']] = $arrData['id']; $arrDatax[1][$arrData['id']] = $arrData['img']; $arrDatax[2][$arrData['id']] = $arrData['title']; $arrDatax[3][$arrData['id']] = $arrData['price']; $arrDatax[4][$arrData['id']] = 1; //判断是否有SESSION存在,有则在数组后添加,没有则直接存 if(empty($_SESSION['shopcar'])){ $_SESSION['shopcar'] = $arrDatax; header('Location:index.php'); }else{ //第一次购物之后的购物 //重新取出来,防止数据覆盖 $arrDataz = $_SESSION['shopcar']; if(in_array($id,$arrDataz[0])){ $arrDataz[4][$arrData['id']] += 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.php'); }else{ $arrDataz[0][$arrData['id']] = $arrData['id']; $arrDataz[1][$arrData['id']] = $arrData['img']; $arrDataz[2][$arrData['id']] = $arrData['title']; $arrDataz[3][$arrData['id']] = $arrData['price']; $arrDataz[4][$arrData['id']] = 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.php'); } } }else{ echo "购物车没有商品!"; }

spcar.php

 $value) { if($id == $value['id']){ $arrData = $arrPro[$key]; } } //用一个新的二维数组把商品信息存起来 $arrDatax[0][$arrData['id']] = $arrData['id']; $arrDatax[1][$arrData['id']] = $arrData['img']; $arrDatax[2][$arrData['id']] = $arrData['title']; $arrDatax[3][$arrData['id']] = $arrData['price']; $arrDatax[4][$arrData['id']] = 1; //判断是否有SESSION存在,有则在数组后添加,没有则直接存 if(empty($_SESSION['shopcar'])){ $_SESSION['shopcar'] = $arrDatax; header('Location:index.php'); }else{ //第一次购物之后的购物 //重新取出来,防止数据覆盖 $arrDataz = $_SESSION['shopcar']; if(in_array($id,$arrDataz[0])){ $arrDataz[4][$arrData['id']] += 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.php'); }else{ $arrDataz[0][$arrData['id']] = $arrData['id']; $arrDataz[1][$arrData['id']] = $arrData['img']; $arrDataz[2][$arrData['id']] = $arrData['title']; $arrDataz[3][$arrData['id']] = $arrData['price']; $arrDataz[4][$arrData['id']] = 1; $_SESSION['shopcar'] = $arrDataz; header('Location:index.php'); } } }else{ echo "购物车没有商品!"; }

delete.php


      

Related learning recommendations:PHP Programming from beginner to master

The above is the detailed content of How to implement shopping cart function in PHP session. For more information, please follow other related articles on the PHP Chinese website!

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