这篇文章主要为大家详细介绍了php session实现购物车功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码
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
本书以培养高级网站建设与管理人才为目标,内容循序渐进,由浅入深,通过大量的实例系统全面地介绍了Linux+PHP+MySQL环境下的网络后台开发技术。本书详尽分析了近30个典型案例。包括计数器、网站流量统计、留言板、论坛系统、聊天室、投票与调查、用户管理、新闻发布系统、广告轮播、购物系统等等,力求让读者通过对案例的学习,轻松掌握PHP和MySQL的编程精要,迅速掌握网络后台开发技巧。 本书适
立即学习“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










