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如何使用Eloquent进行子查询  Python数据仓库与ETL构建实战_Airflow调度流程详解  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  海南网站制作公司有哪些,海口网是哪家的?  如何在建站宝盒中设置产品搜索功能?  Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?  WordPress 子目录安装中正确处理脚本路径的完整指南  如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)  做企业网站制作流程,企业网站制作基本流程有哪些?  EditPlus中的正则表达式 实战(4)  JavaScript中如何操作剪贴板_ClipboardAPI怎么用  Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议  如何用免费手机建站系统零基础打造专业网站?  EditPlus中的正则表达式实战(5)  android nfc常用标签读取总结  如何为不同团队 ID 动态生成多个非值班状态按钮  VIVO手机上del键无效OnKeyListener不响应的原因及解决方法  如何在云虚拟主机上快速搭建个人网站?  中山网站推广排名,中山信息港登录入口?  Laravel如何实现一对一模型关联?(Eloquent示例)  Laravel如何使用Laravel Vite编译前端_Laravel10以上版本前端静态资源管理【教程】  Laravel怎么创建控制器Controller_Laravel路由绑定与控制器逻辑编写【指南】  Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】  WEB开发之注册页面验证码倒计时代码的实现  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  javascript基于原型链的继承及call和apply函数用法分析  车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?  如何快速选择适合个人网站的云服务器配置?  Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明  大同网页,大同瑞慈医院官网?  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  Laravel如何监控和管理失败的队列任务_Laravel失败任务处理与监控  焦点电影公司作品,电影焦点结局是什么?  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程  Laravel怎么使用Intervention Image库处理图片上传和缩放  Python自然语言搜索引擎项目教程_倒排索引查询优化案例  如何快速辨别茅台真假?关键步骤解析  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  Laravel怎么判断请求类型_Laravel Request isMethod用法  javascript如何操作浏览器历史记录_怎样实现无刷新导航  javascript事件捕获机制【深入分析IE和DOM中的事件模型】  教学论文网站制作软件有哪些,写论文用什么软件 ?  EditPlus中的正则表达式 实战(2)  详解Nginx + Tomcat 反向代理 负载均衡 集群 部署指南  如何快速完成中国万网建站详细流程?  jimdo怎样用html5做选项卡_jimdo选项卡html5实现与切换效果【指南】  Laravel如何为API编写文档_Laravel API文档生成与维护方法