PHP session实现购物车功能
发布时间 - 2026-01-11 01:47:02 点击率:次在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码

common.php
<?php
header("content-type:text/html;charset=utf-8");
$arrPro = array(
array('id'=>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
<?php
header("content-type:text/html;charset=utf-8");
require 'common.php';
session_start();
$sum = 0;
$class = "";
//判断左上角购物车的样式显示
if(!empty($_SESSION['shopcar'])){
$data = $_SESSION['shopcar'];
$sum = array_sum($data[4]);
$class = "on";
//右上角圆点
if(empty($data[0])){
$class = "";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品展示</title>
<style type="text/css">
section{
width:1032px;
height: 700px;
margin: 40px auto;
}
.top{
float: right;
position: relative;
width: 190px;
height: 34px;
border: 1px solid #ccc;
margin-right: 32px;
text-align: center;
line-height: 34px;
border-radius: 4px;
transition: all .3s linear;
-moz-transition: all .3s linear;
-webkit-transition: all .3s linear;
}
.top a{
color: #d00;
}
.top:hover{
width: 210px;
color: #fff;
font-weight: bold;
background-color: #d00;
border-radius: 6px;
}
.top:hover a{
color: #fff;
}
.top:hover span{
background-color: #fff;
color: #d00;
}
.top:hover .star{
right:150px;
top: 0;
font-size: 14px;
color: #ff0;
transform: rotate(1080deg);
}
.top span{
position: absolute;
top:2px;
right: 40px;
width: 18px;
height: 18px;
font-weight: bold;
border-radius: 9px;
line-height: 18px;
text-align: center;
font-size: 12px;
font-weight: border;
color: #fff;
}
.star{
color: #fff;
font-size: 48px;
font-style: normal;
position: absolute;
right:530px;
top:186px;
transform: rotate(60deg);
transition: all .3s ease;
}
.on{
background-color: #e00;
}
.list{
margin: 20px 20px;
padding: 36px 0;
list-style: none;
}
a{
display: block;
color: #757575;
text-decoration: none;
}
.list li{
float: left;
height: 246px;
width: 234px;
padding: 10px 0 20px;
margin-right:12px;
margin-top: 20px;
border: 1px solid #ccc;
background: #fff;
-webkit-transition: all .2s linear;
transition: all .2s linear;
}
.list li:hover{
box-shadow: 2px 4px 5px #aaa;
}
.figure{
width: 150px;
height: 150px;
margin: 0 auto 18px;
}
.title{
color: #222;
font-size: 14px;
font-weight: normal;
text-align: center;
}
.price{
margin: 0 10px 10px;
text-align: center;
color: #ff6700;
}
.cart{
margin: 0 15px 5px;
text-align: center;
}
.cart a{
color: #a34;
width: 190px;
height: 24px;
border-radius: 4px;
margin: 0 8px 5px;
text-align: center;
}
.cart a:hover{
color: #eee;
box-shadow: 0 2px 1px #333,0 2px 1px #666;
background-color: #ccc;
background-image: linear-gradient(#33a6b8,#0089a7)
}
.num{
text-align: center;
color: #ff6700;
}
</style>
</head>
<body>
<section>
<div class='top'>
<a href="spcar.php" rel="external nofollow" >我的购物车</a><span class="<?php echo $class;?>"><?php echo $sum;?></span>
<em class='star'>★</em>
</div>
<ul class="list">
<?php foreach ($arrPro as $key => $value):?>
<li>
<div class="figure">
<a href=""><img src=" rel="external nofollow" <?php echo $value['img'];?>" width="150" height="150" alt="小米移动电源5000mAh"></a>
</div>
<h3 class="title">
<a href=""><?php echo $value['title'];?></a>
</h3>
<p class="price"><span class="num">¥<?php echo $value['price'];?></span></p>
<p class='cart'><a href="action.php?id=<?php echo $value['id'];?>" rel="external nofollow" >加入购物车</a></p>
</li>
<?php endforeach;?>
</ul>
<div style='clear:both'></div>
</section>
</body>
</html>
action.php
<?php
if(!empty($_GET['id'])){
require 'common.php';
session_start();
$id = $_GET['id'];
//把所选ID的商品信息遍历出来
foreach ($arrPro as $key => $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
<?php
if(!empty($_GET['id'])){
require 'common.php';
session_start();
$id = $_GET['id'];
//把所选ID的商品信息遍历出来
foreach ($arrPro as $key => $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
<?php
session_start();
if(!empty($_GET['id'])){
$arrData = $_SESSION['shopcar'];
//判断对应的商品ID信息
if(in_array($_GET['id'],$arrData[0])){
unset($arrData[0][$_GET['id']]);
unset($arrData[1][$_GET['id']]);
unset($arrData[2][$_GET['id']]);
unset($arrData[3][$_GET['id']]);
unset($arrData[4][$_GET['id']]);
$_SESSION['shopcar'] = $arrData;
}
header('Location:spcar.php');
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# PHP
# session
# 购物车
# php+pdo实现的购物车类完整示例
# php实现购物车产品删除功能(2)
# php实现产品加入购物车功能(1)
# PHP购物车类Cart.class.php定义与用法示例
# php实现购物车功能(下)
# php实现购物车功能(上)
# 简单的php购物车代码
# 遍历
# 则在
# 所选
# 判断是否
# 放在
# 大家多多
# 各部分
# 圆点
# 滑板车
# 连接数据库
# 组里
# data
# array_sum
# shopcar
# DOCTYPE
# meta
# head
# en
# lang
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何配置和使用缓存?(Redis代码示例)
高端云建站费用究竟需要多少预算?
Win11任务栏卡死怎么办 Windows11任务栏无反应解决方法【教程】
laravel怎么为应用开启和关闭维护模式_laravel应用维护模式开启与关闭方法
今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】
如何生成腾讯云建站专用兑换码?
如何用已有域名快速搭建网站?
Laravel如何使用Livewire构建动态组件?(入门代码)
php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】
高性能网站服务器配置指南:安全稳定与高效建站核心方案
Laravel用户密码怎么加密_Laravel Hash门面使用教程
laravel怎么配置Redis作为缓存驱动_laravel Redis缓存配置教程
如何用搬瓦工VPS快速搭建个人网站?
图册素材网站设计制作软件,图册的导出方式有几种?
Laravel观察者模式如何使用_Laravel Model Observer配置
Laravel如何创建自定义Artisan命令?(代码示例)
Win11摄像头无法使用怎么办_Win11相机隐私权限开启教程【详解】
Win11关机界面怎么改_Win11自定义关机画面设置【工具】
Laravel如何集成Inertia.js与Vue/React?(安装配置)
如何快速建站并高效导出源代码?
Laravel如何与Docker(Sail)协同开发?(环境搭建教程)
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
如何用5美元大硬盘VPS安全高效搭建个人网站?
怎么用AI帮你设计一套个性化的手机App图标?
香港服务器部署网站为何提示未备案?
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
实例解析Array和String方法
简单实现jsp分页
手机网站制作与建设方案,手机网站如何建设?
Laravel如何自定义分页视图?(Pagination示例)
深入理解Android中的xmlns:tools属性
桂林网站制作公司有哪些,桂林马拉松怎么报名?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
javascript中闭包概念与用法深入理解
Laravel如何使用集合(Collections)进行数据处理_Laravel Collection常用方法与技巧
网站建设整体流程解析,建站其实很容易!
如何用好域名打造高点击率的自主建站?
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
如何用AI一键生成爆款短视频文案?小红书AI文案写作指令【教程】
高防服务器租用指南:配置选择与快速部署攻略
惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?
微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】
如何挑选优质建站一级代理提升网站排名?
Linux虚拟化技术教程_KVMQEMU虚拟机安装与调优
Laravel项目怎么部署到Linux_Laravel Nginx配置详解
Laravel怎么配置不同环境的数据库_Laravel本地测试与生产环境动态切换【方法】
Win11应用商店下载慢怎么办 Win11更改DNS提速下载【修复】
Laravel怎么使用Blade模板引擎_Laravel模板继承与Component组件复用【手册】
JavaScript数据类型有哪些_如何准确判断一个变量的类型

