一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP session實現(xiàn)購物車功能

PHP session實現(xiàn)購物車功能

2020-05-07 13:35Jack-小俊 PHP教程

這篇文章主要為大家詳細介紹了PHP session實現(xiàn)購物車功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在wamp環(huán)境下,用PHPsession會話控制完成購物車的效果,數(shù)據存放在數(shù)組里練習,沒有連接數(shù)據庫,效果不錯,簡單易懂,以下是各部分的代碼

common.php

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?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" id="codetool">

action.php

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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];
    }
  }
 
  //用一個新的二維數(shù)組把商品信息存起來
  $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存在,有則在數(shù)組后添加,沒有則直接存
  if(empty($_SESSION['shopcar'])){
    $_SESSION['shopcar'] = $arrDatax;
    header('Location:index.php');
  }else{
    //第一次購物之后的購物
    //重新取出來,防止數(shù)據覆蓋
    $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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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];
    }
  }
 
  //用一個新的二維數(shù)組把商品信息存起來
  $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存在,有則在數(shù)組后添加,沒有則直接存
  if(empty($_SESSION['shopcar'])){
    $_SESSION['shopcar'] = $arrDatax;
    header('Location:index.php');
  }else{
    //第一次購物之后的購物
    //重新取出來,防止數(shù)據覆蓋
    $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
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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');
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 手机在线观看国产精选免费 | 日本黄a三级三级三级 | 免费在线观看视频 | 91制片厂 果冻传媒 天美传媒 | 91人成尤物在线 | 日韩欧美一区二区三区免费看 | 亚洲 欧美 国产 日韩 字幕 | 亚洲天堂2016 | 成人在线视频国产 | 1024香蕉视频| 色多多视频在线 | 五月天色网站 | 亚洲视频在线观看免费视频 | 小草观看免费高清视频 | 成年人免费在线看的惊悚动作片 | 小伙无套内射老女人 | 91交换论坛| 四虎精品成人免费影视 | 国产精品免费精品自在线观看 | 性夜影院爽黄A爽免费动漫 性色欲情网站IWWW九文堂 | 美女被狂揉下部羞羞动漫 | 婷婷伊人综合亚洲综合网 | 免费观看视频在线 | 精品91一区二区三区 | 日本艳鉧动漫1~6完整版在 | 国产实拍会所女技师在线 | 日韩欧美一区二区三区视频 | 加勒比成人 | 亚洲毛片网 | 国产综合色在线视频区色吧图片 | x8x8国产在线观看2021 | 午夜伦伦电影理论片费看 | 好爽轻点太大了太深了 | 国产精品自在线拍 | 波多野结衣之双方调教在线观看 | 192.168.191| 久久视频在线视频 | 毛毛片在线 | 国产精品99精品久久免费 | 极品ts赵恩静和直男激战啪啪 | 人阁色第四影院在线观看 |
          • <td id="sckao"></td>