温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在PHP中使用session实现一个购物车功能

发布时间:2021-02-08 17:29:58 阅读:254 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

如何在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;
    height700px;
    margin40px auto;
  }
  .top{
    float: right;
    position: relative;
    width190px;
    height34px;
    border1px solid #ccc;
    margin-right32px;
    text-align: center;
    line-height34px;
    border-radius4px;
    transition: all .3s linear;
    -moz-transition: all .3s linear;
    -webkit-transition: all .3s linear;
  }
  .top a{
    color#d00;
  }
  .top:hover{
    width210px;
    color#fff;
    font-weight: bold;
    background-color#d00;
    border-radius6px;
  }
  .top:hover a{
    color#fff;
  }
  .top:hover span{
    background-color#fff;
    color#d00;
  }
  .top:hover .star{
    right:150px;
    top0;
    font-size14px;
    color#ff0;
    transformrotate(1080deg);
  }
  .top span{
    position: absolute;
    top:2px;
    right40px;
    width18px;
    height18px;
    font-weight: bold;
    border-radius9px;
    line-height18px;
    text-align: center;
    font-size12px;
    font-weight: border;
    color#fff;
  }
  .star{
    color#fff;
    font-size48px;
    font-style: normal;
    position: absolute;
    right:530px;
    top:186px;
    transformrotate(60deg);
    transition: all .3s ease;
  }
  .on{
    background-color#e00;
  }
  .list{
    margin20px 20px;
    padding36px 0;
    list-style: none;
  }  
  a{
    display: block;
    color#757575;
    text-decoration: none;
  }
  .list li{
    float: left;
    height246px;
    width234px;
    padding10px 0 20px;
    margin-right:12px;
    margin-top20px;
    border1px solid #ccc;
    background#fff;
    -webkit-transition: all .2s linear;
    transition: all .2s linear;
  }
  .list li:hover{
    box-shadow2px 4px 5px #aaa;
  }
  .figure{
    width150px;
    height150px;
    margin0 auto 18px;
  }
  .title{
    color#222;
    font-size14px;
    font-weight: normal;
    text-align: center;
  }
  .price{
    margin0 10px 10px;
    text-align: center;
    color#ff6700;
  }
  .cart{
    margin0 15px 5px;
    text-align: center;
  }
  .cart a{
    color#a34;
    width190px;
    height24px;
    border-radius4px;
    margin0 8px 5px;
    text-align: center;
  }
  .cart a:hover{
    color#eee;
    box-shadow0 2px 1px #333,0 2px 1px #666;
    background-color#ccc;
    background-imagelinear-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> 
    <h4 class="title"> 
    <a href=""><?php echo $value['title'];?></a>
    </h4> 
    <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实现一个购物车功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×