PHP session如何实现购物车功能

在wamp环境下,用PHP的session会话控制完成购物车的效果,数据存放在数组里练习,没有连接数据库,效果不错,简单易懂,以下是各部分的代码。

common.php

  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. $arrPro = array(
  4. array('id'=>1,'img'=>'img/1.jpg','title'=>'小米移动电源5000mAh','price'=>49),
  5. array('id'=>2,'img'=>'img/2.jpg','title'=>'20000mAh小米移动电源2','price'=>149),
  6. array('id'=>3,'img'=>'img/3.jpg','title'=>'小米圈铁耳机Pro','price'=>129),
  7. array('id'=>4,'img'=>'img/4.jpg','title'=>'小米家电动滑板车','price'=>1999),
  8. array('id'=>5,'img'=>'img/5.jpg','title'=>'小米笔记本','price'=>3499),
  9. array('id'=>6,'img'=>'img/6.jpg','title'=>'米家LED智能台灯','price'=>169),
  10. array('id'=>7,'img'=>'img/7.jpg','title'=>'小米体重秤','price'=>99),
  11. array('id'=>8,'img'=>'img/8.png','title'=>'小米电视3s 48英寸','price'=>2599)
  12. );

index.php

  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. require 'common.php';
  4. session_start();
  5. $sum = 0;
  6. $class = "";
  7. //判断左上角购物车的样式显示
  8. if(!emptyempty($_SESSION['shopcar'])){
  9. $data = $_SESSION['shopcar'];
  10. $sum = array_sum($data[4]);
  11. $class = "on";
  12. //右上角圆点
  13. if(emptyempty($data[0])){
  14. $class = "";
  15. }
  16. }
  17. ?>
  18. <!DOCTYPE html>
  19. <html >
  20. <head>
  21. <meta charset="UTF-8">
  22. <title>商品展示</title>
  23. <style type="text/css">
  24. section{
  25. width:1032px;
  26. height: 700px;
  27. margin: 40px auto;
  28. }
  29. .top{
  30. float: right;
  31. position: relative;
  32. width: 190px;
  33. height: 34px;
  34. border: 1px solid #ccc;
  35. margin-right: 32px;
  36. text-align: center;
  37. line-height: 34px;
  38. border-radius: 4px;
  39. transition: all .3s linear;
  40. -moz-transition: all .3s linear;
  41. -webkit-transition: all .3s linear;
  42. }
  43. .top a{
  44. color: #d00;
  45. }
  46. .top:hover{
  47. width: 210px;
  48. color: #fff;
  49. font-weight: bold;
  50. background-color: #d00;
  51. border-radius: 6px;
  52. }
  53. .top:hover a{
  54. color: #fff;
  55. }
  56. .top:hover span{
  57. background-color: #fff;
  58. color: #d00;
  59. }
  60. .top:hover .star{
  61. right:150px;
  62. top: 0;
  63. font-size: 14px;
  64. color: #ff0;
  65. transform: rotate(1080deg);
  66. }
  67. .top span{
  68. position: absolute;
  69. top:2px;
  70. right: 40px;
  71. width: 18px;
  72. height: 18px;
  73. font-weight: bold;
  74. border-radius: 9px;
  75. line-height: 18px;
  76. text-align: center;
  77. font-size: 12px;
  78. font-weight: border;
  79. color: #fff;
  80. }
  81. .star{
  82. color: #fff;
  83. font-size: 48px;
  84. font-style: normal;
  85. position: absolute;
  86. right:530px;
  87. top:186px;
  88. transform: rotate(60deg);
  89. transition: all .3s ease;
  90. }
  91. .on{
  92. background-color: #e00;
  93. }
  94. .list{
  95. margin: 20px 20px;
  96. padding: 36px 0;
  97. list-style: none;
  98. }
  99. a{
  100. display: block;
  101. color: #757575;
  102. text-decoration: none;
  103. }
  104. .list li{
  105. float: left;
  106. height: 246px;
  107. width: 234px;
  108. padding: 10px 0 20px;
  109. margin-right:12px;
  110. margin-top: 20px;
  111. border: 1px solid #ccc;
  112. background: #fff;
  113. -webkit-transition: all .2s linear;
  114. transition: all .2s linear;
  115. }
  116. .list li:hover{
  117. box-shadow: 2px 4px 5px #aaa;
  118. }
  119. .figure{
  120. width: 150px;
  121. height: 150px;
  122. margin: 0 auto 18px;
  123. }
  124. .title{
  125. color: #222;
  126. font-size: 14px;
  127. font-weight: normal;
  128. text-align: center;
  129. }
  130. .price{
  131. margin: 0 10px 10px;
  132. text-align: center;
  133. color: #ff6700;
  134. }
  135. .cart{
  136. margin: 0 15px 5px;
  137. text-align: center;
  138. }
  139. .cart a{
  140. color: #a34;
  141. width: 190px;
  142. height: 24px;
  143. border-radius: 4px;
  144. margin: 0 8px 5px;
  145. text-align: center;
  146. }
  147. .cart a:hover{
  148. color: #eee;
  149. box-shadow: 0 2px 1px #333,0 2px 1px #666;
  150. background-color: #ccc;
  151. background-image: linear-gradient(#33a6b8,#0089a7)
  152. }
  153. .num{
  154. text-align: center;
  155. color: #ff6700;
  156. }
  157. </style>
  158. </head>
  159. <body>
  160. <section>
  161. <p class='top'>
  162. <a href="spcar.php" rel="external nofollow" >我的购物车</a><span class="<?php echo $class;?>"><?php echo $sum;?></span>
  163. <em class='star'>★</em>
  164. </p>
  165. <ul class="list">
  166. <?php foreach ($arrPro as $key => $value):?>
  167. <li>
  168. <p class="figure">
  169. <a href=""><img src=" rel="external nofollow" <?php echo $value['img'];?>" width="150" height="150" alt="小米移动电源5000mAh"></a>
  170. </p>
  171. <h3 class="title">
  172. <a href=""><?php echo $value['title'];?></a>
  173. </h3>
  174. <p class="price"><span class="num">¥<?php echo $value['price'];?></span></p>
  175. <p class='cart'><a href="action.php?id'];?>" rel="external nofollow" >加入购物车</a></p>
  176. </li>
  177. <?php endforeach;?>
  178. </ul>
  179. <p ></p>
  180. </section>
  181. </body>
  182. </html>

action.php

  1. <?php
  2. if(!emptyempty($_GET['id'])){
  3. require 'common.php';
  4. session_start();
  5. $id = $_GET['id'];
  6. //把所选ID的商品信息遍历出来
  7. foreach ($arrPro as $key => $value) {
  8. if($id == $value['id']){
  9. $arrData = $arrPro[$key];
  10. }
  11. }
  12. //用一个新的二维数组把商品信息存起来
  13. $arrDatax[0][$arrData['id']] = $arrData['id'];
  14. $arrDatax[1][$arrData['id']] = $arrData['img'];
  15. $arrDatax[2][$arrData['id']] = $arrData['title'];
  16. $arrDatax[3][$arrData['id']] = $arrData['price'];
  17. $arrDatax[4][$arrData['id']] = 1;
  18. //判断是否有SESSION存在,有则在数组后添加,没有则直接存
  19. if(emptyempty($_SESSION['shopcar'])){
  20. $_SESSION['shopcar'] = $arrDatax;
  21. header('Location:index.php');
  22. }else{
  23. //第一次购物之后的购物
  24. //重新取出来,防止数据覆盖
  25. $arrDataz = $_SESSION['shopcar'];
  26. if(in_array($id,$arrDataz[0])){
  27. $arrDataz[4][$arrData['id']] += 1;
  28. $_SESSION['shopcar'] = $arrDataz;
  29. header('Location:index.php');
  30. }else{
  31. $arrDataz[0][$arrData['id']] = $arrData['id'];
  32. $arrDataz[1][$arrData['id']] = $arrData['img'];
  33. $arrDataz[2][$arrData['id']] = $arrData['title'];
  34. $arrDataz[3][$arrData['id']] = $arrData['price'];
  35. $arrDataz[4][$arrData['id']] = 1;
  36. $_SESSION['shopcar'] = $arrDataz;
  37. header('Location:index.php');
  38. }
  39. }
  40. }else{
  41. echo "购物车没有商品!";
  42. }

spcar.php

  1. <?php
  2. if(!emptyempty($_GET['id'])){
  3. require 'common.php';
  4. session_start();
  5. $id = $_GET['id'];
  6. //把所选ID的商品信息遍历出来
  7. foreach ($arrPro as $key => $value) {
  8. if($id == $value['id']){
  9. $arrData = $arrPro[$key];
  10. }
  11. }
  12. //用一个新的二维数组把商品信息存起来
  13. $arrDatax[0][$arrData['id']] = $arrData['id'];
  14. $arrDatax[1][$arrData['id']] = $arrData['img'];
  15. $arrDatax[2][$arrData['id']] = $arrData['title'];
  16. $arrDatax[3][$arrData['id']] = $arrData['price'];
  17. $arrDatax[4][$arrData['id']] = 1;
  18. //判断是否有SESSION存在,有则在数组后添加,没有则直接存
  19. if(emptyempty($_SESSION['shopcar'])){
  20. $_SESSION['shopcar'] = $arrDatax;
  21. header('Location:index.php');
  22. }else{
  23. //第一次购物之后的购物
  24. //重新取出来,防止数据覆盖
  25. $arrDataz = $_SESSION['shopcar'];
  26. if(in_array($id,$arrDataz[0])){
  27. $arrDataz[4][$arrData['id']] += 1;
  28. $_SESSION['shopcar'] = $arrDataz;
  29. header('Location:index.php');
  30. }else{
  31. $arrDataz[0][$arrData['id']] = $arrData['id'];
  32. $arrDataz[1][$arrData['id']] = $arrData['img'];
  33. $arrDataz[2][$arrData['id']] = $arrData['title'];
  34. $arrDataz[3][$arrData['id']] = $arrData['price'];
  35. $arrDataz[4][$arrData['id']] = 1;
  36. $_SESSION['shopcar'] = $arrDataz;
  37. header('Location:index.php');
  38. }
  39. }
  40. }else{
  41. echo "购物车没有商品!";
  42. }

delete.php

  1. <?php
  2. session_start();
  3. if(!emptyempty($_GET['id'])){
  4. $arrData = $_SESSION['shopcar'];
  5. //判断对应的商品ID信息
  6. if(in_array($_GET['id'],$arrData[0])){
  7. unset($arrData[0][$_GET['id']]);
  8. unset($arrData[1][$_GET['id']]);
  9. unset($arrData[2][$_GET['id']]);
  10. unset($arrData[3][$_GET['id']]);
  11. unset($arrData[4][$_GET['id']]);
  12. $_SESSION['shopcar'] = $arrData;
  13. }
  14. header('Location:spcar.php');
  15. }