Blogger Information
Blog 8
fans 0
comment 0
visits 14492
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 使用session实现购物车效果(点击按钮添加数据,根据数据生成列表,删除选中的商品,点击按钮时计算金额)
熟悉的新风景
Original
1349 people have browsed it

效果图

代码很多,截取一部分

加入购物车(点击按钮添加session数据)

在这里插入图片描述

  1. // 加入购物车
  2. function addshopping() {
  3. <?php
  4. $db = db($_GET['db']);
  5. $id = $formatData['id'];
  6. $user = $formatData['user'];
  7. // 当购物车没有时,即没有session数据时
  8. if (empty($_SESSION['gwc'])) {
  9. $arr = array(array('user' => $user, 'db' => $db, 'id' => $id, 'num' => 1));
  10. $_SESSION['gwc'] = $arr;
  11. } else {
  12. // 当购物车有数据时
  13. $arr = $_SESSION["gwc"];
  14. $order = false;
  15. foreach ($arr as $key => $value) {
  16. // 如果数据中有了这个数据
  17. if ($value['user'] == $user && $value['db'] == $db && $value['id'] == $id) {
  18. $index = $key; //保存这个相同数据的下标,以便操作
  19. $order = true;
  20. //定义一个变量,看是佛有相同的数据,需要注意的是,不能再这个if语句中else,否则后面的语句
  21. //虽然会使数量加一,但是还是会多创建一条数据,为什么,自己想想就能明白,只是当时当局者迷
  22. }
  23. }
  24. if ($order) {
  25. // 如果该商品在购物车存在,让数量加1
  26. $arr[$index]['num']++;
  27. } else {
  28. // 如果该商品购物车里面不存在,造一个一维数组扔到二维里面
  29. $arr[] = array('user' => $user, 'db' => $db, 'id' => $id, 'num' => 1);
  30. }
  31. $_SESSION["gwc"] = $arr;
  32. }
  33. ?>
  34. console.log('addshopping');
  35. }

根据数据加载列表

  1. <!-- 购物车列表 -->
  2. <?php
  3. foreach ($_SESSION['gwc'] as $dataList) {
  4. $clean = array();
  5. $clean['user'] = $dataList['user'];
  6. $clean['db'] = $dataList['db'];
  7. $clean['id'] = $dataList['id'];
  8. $clean['num'] = $dataList['num'];
  9. $res = $conn->fetchRow("SELECT * FROM {$clean['db']} WHERE id={$clean['id']}");
  10. $clean['title'] = $res['title'];
  11. $clean['price'] = $res['price'];
  12. $clean['face'] = $res['face'];
  13. ?>
  14. <div class="weui-content">
  15. <div class="weui-panel weui-panel_access">
  16. <div class="weui-panel__hd"><span><?php echo $clean['user']; ?> </span><a href="javascript:void(0)" class="wy-dele"><!-- 删除 --></a></div>
  17. <div class="weui-panel__bd">
  18. <div class="weui-media-box_appmsg pd-10">
  19. <div class="weui-media-box__hd check-w weui-cells_checkbox">
  20. <label class="weui-check__label" for="cart-<?php echo $clean['db'] . '-' . $clean['id'] ?>">
  21. <div class="weui-cell__hd cat-check">
  22. <input type="checkbox" class="weui-check" name="cartpro" id="cart-<?php echo $clean['db'] . '-' . $clean['id'] ?>"><i class="weui-icon-checked"></i>
  23. </div>
  24. </label>
  25. </div>
  26. <div class="weui-media-box__hd"><a href="pro_info.php?db=<?php echo $clean['db']; ?>&id=<?php echo $clean['id']; ?>"><img class="weui-media-box__thumb" src="<?php echo $clean['face']; ?>" alt="" style="height: 100%"></a></div>
  27. <div class="weui-media-box__bd">
  28. <h1 class="weui-media-box__desc"><a href="pro_info.php?db=<?php echo $clean['db']; ?>&id=<?php echo $clean['id']; ?>" class="ord-pro-link"><?php echo $clean['title']; ?></a></h1>
  29. <p class="weui-media-box__desc">规格:<span>红色</span><span>23</span></p>
  30. <div class="clear mg-t-10">
  31. <div class="wy-pro-pri fl">¥<em class="num font-15 price"><?php echo $clean['price']; ?></em></div>
  32. <div class="pro-amount fr">
  33. <!-- 的数量 -->
  34. <input type="hidden" name="" class="datanum" value="<?php echo $clean['num']; ?>">
  35. <!-- 提交删除的数据 -->
  36. <input type="hidden" name="" class="rmdata" value="shopping.php?db=<?php echo $clean['db']; ?>&id=<?php echo $clean['id'];?>">
  37. <div class="Spinner"></div>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. <?php }
  46. ?>

删除选中的商品

  1. // echo '<pre>';
  2. // print_r($_SESSION['gwc']);
  3. // echo '</pre>';
  4. if(isset($_GET['db']) && isset($_GET['id'])){
  5. $dataArr = $_SESSION['gwc'];
  6. foreach ($dataArr as $key => $value) {
  7. if($value['db'] == $_GET['db'] && $value['id'] == $_GET['id']){
  8. unset($dataArr[$key]);
  9. }
  10. }
  11. $_SESSION['gwc'] = $dataArr;
  12. header("Location:shopping.php");
  13. }
  14. // echo '<pre>';
  15. // print_r( $dataArr);
  16. // echo '</pre>';

金额计算

  1. <!-- 金额计算 -->
  2. <script>
  3. window.onload = function() {
  4. var cartproAll = document.querySelectorAll('input[name=cartpro]');
  5. var datanum = document.querySelectorAll('.datanum');
  6. var numAll = document.querySelectorAll('.Amount');
  7. var DisDe = document.querySelectorAll('.DisDe');
  8. var Increase = document.querySelectorAll('.Increase');
  9. // 数量的多少
  10. numAll.forEach((item,i) => {
  11. item.value = datanum[i].value;
  12. })
  13. // 点击加号
  14. DisDe.forEach((item, i) => {
  15. item.onclick = function() {
  16. computedPrice();
  17. }
  18. })
  19. // 点击减号
  20. Increase.forEach((item, i) => {
  21. item.onclick = function() {
  22. computedPrice();
  23. }
  24. })
  25. // 点击单选时计算金额
  26. cartproAll.forEach((item, i) => {
  27. item.onclick = function() {
  28. computedPrice();
  29. }
  30. })
  31. }
  32. function computedPrice(){
  33. var cartproAll = document.querySelectorAll('input[name=cartpro]'); //获取是否点击
  34. var price = document.querySelectorAll('.price'); //价钱
  35. var price_total = document.querySelector('.price_total'); //价钱总数
  36. var numAll = document.querySelectorAll('.Amount'); //数量
  37. var total = 0;
  38. price_total.innerHTML = 0;
  39. cartproAll.forEach((item, i) => {
  40. if (item.checked == true) {
  41. total += parseFloat(price[i].innerHTML) * parseFloat(numAll[i].value);
  42. }
  43. })
  44. price_total.innerHTML = total;
  45. }
  46. </script>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments