如何在PHP中使用session实现一个购物车功能
创始人
2024-11-28 00:42:12
0次

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






  
  商品展示
  
  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;
  }
  


  
     我的购物车">   ★   
      $value):?>   
  •           " width="150" height="150" alt="小米移动电源5000mAh">      
  •                        ¥

        " rel="external nofollow" >加入购物车

               
      

    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

                    
                

    相关内容

    热门资讯

    空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
    空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
    格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
    空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
    格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
    品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
    空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
    空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
    中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
    中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...