加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP session怎么实现购物车功能

发布时间:2022-07-21 14:19:40 所属栏目:PHP教程 来源:互联网
导读:在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码 $arrPro = array( array(id=1,img=img/1.jpg,title=小米移动电源5000mAh,price=49), array(id=2,img=img/2.jpg,title
  在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码
 
  $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)
 
 
  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])){
 
  <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;
 
 
      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{
 
      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;
 
      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>
 
    <p class='top'>
 
    <a href="spcar.php" rel="external nofollow" >我的购物车</a><span class="<?php echo $class;?>"><?php echo $sum;?></span>
 
    <em class='star'>★</em>
 
    </p>
 
    <ul class="list">
 
    <?php foreach ($arrPro as $key => $value):?>
 
    <li>
 
      <p class="figure">
 
      <a href=""><img src=" rel="external nofollow" <?php echo $value['img'];?>" width="150" height="150" alt="小米移动电源5000mAh"></a>
 
      </p>
 
      <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

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读