PHP使用cookie实现简单的购物车功能

https://blog.csdn.net/a345263950/article/details/81907275

学习php过程中尝试用cookie做一下购物车,参考和学习网上的做法,主要用cookie记录一个二维数组

----------------------------------------------------

array(

'商品id1'=>array('名字','库存等'),

'商品id2'=>array('名字','库存等')

);

商品页面

function.php 函数页面

 
  1. <?php

  2. // 加入购物车

  3. function addCart($gid, $gname)

  4. {

  5. // 第一次点击创建一个数组

  6. if (empty(unserialize($_COOKIE['cart']))) {

  7. $arr = array($gid => array($gname, 1));

  8. setcookie('cart', serialize($arr));

  9. } else {

  10. $arr = unserialize($_COOKIE['cart']);

  11.  
  12. // 如果没有这个商品,添加一个商品数组

  13. if ($arr[$gid][0] != $gname) {

  14. $arr[$gid] = array($gname, 1);

  15. setcookie('cart', serialize($arr));

  16. } else { //如果商品存在数量加1

  17. $arr[$gid][1]++;

  18. setcookie('cart', serialize($arr));

  19. }

  20. }

  21. }

  22. // 删除一个商品

  23. // $del是get请求的删除id $del = $_GET['del'];

  24. function delCart($shop_car, $del)

  25. {

  26. unset($shop_car[$del]);

  27. setcookie('cart', serialize($shop_car));

  28. echo '<script>window.location.href = "showcar.php"</script>';

  29. }

  30.  
  31. //清空购物车全部

  32. function delAll()

  33. {

  34. if (isset($_GET['clean'])) {

  35. if ($_GET['clean'] == 'all') {

  36. setcookie('cart', '', time() - 3600);

  37. echo '<script>alert("清空成功");window.location.href="showcar.php";</script>';

  38. }

  39. }

  40. }

  41.  
  42.  
  43. // 计算总价格

  44. function totalPrice($shop_car)

  45. {

  46. require_once 'mysqli.php';

  47. $price = 0;

  48. // $kk是随便定义的一个数组名字

  49. foreach ($shop_car as $kk) {

  50. $sql = 'select price from one where gname="' . $kk[0] . '"';

  51. $select = $db->query($sql);

  52. $result = $select->fetch_object();

  53. $price += $kk[1] * $result->price;

  54. }

  55. return $price;

  56. }

  57. ```

  58.  
  59. shop.php 商品页面

  60. ```

  61. <?php

  62. include 'mysqli.php';

  63. $select = $db->query('select * from one');

  64. $result = $select->fetch_assoc();

  65.  
  66. while ($result) {

  67. echo '<div style="float:left; width:30%;margin-left:1.5%;text-align:center">

  68. <img src="' . $result['url'] . '" style="width:200px;height:200px"/>';

  69.  
  70. echo '<p>商品id: ' . $result['id'] . ' 名称: ' . $result['gname'] . ' 价格: $' . $result['price'] . '</p>';

  71.  
  72. echo '<a href="addcar.php?gid=' . $result['id'] . '">加入购物车</a></div>';

  73. $result = $select->fetch_assoc();

  74. }

  75.  
  76. echo '<div style="margin:0 auto; width:150px; font-size:22px">

  77. <a target="_blank" href="showcar.php">查看购物车</a>

  78. </div>';

shop.php 商品页面

 
  1. <?php

  2. include 'mysqli.php';

  3. $select = $db->query('select * from one');

  4. $result = $select->fetch_assoc();

  5.  
  6. while ($result) {

  7. echo '<div style="float:left; width:30%;margin-left:1.5%;text-align:center">

  8. <img src="' . $result['url'] . '" style="width:200px;height:200px"/>';

  9.  
  10. echo '<p>商品id: ' . $result['id'] . ' 名称: ' . $result['gname'] . ' 价格: $' . $result['price'] . '</p>';

  11.  
  12. echo '<a href="addcar.php?gid=' . $result['id'] . '">加入购物车</a></div>';

  13. $result = $select->fetch_assoc();

  14. }

  15.  
  16. echo '<div style="margin:0 auto; width:150px; font-size:22px">

  17. <a target="_blank" href="showcar.php">查看购物车</a>

  18. </div>';

addcar.php 加入购物车

 
  1. <?php

  2. ini_set('error_reporting', 'E_ALL & ~E_NOTICE');

  3. require_once 'mysqli.php';

  4. require_once 'function.php';

  5. $gid = $_GET['gid'];

  6.  
  7. $sql = 'select gname from one where id=' . $gid;

  8. $select = $db->query($sql);

  9.  
  10. // 查询错误返回

  11. if (!$select) {

  12. header('location:shop.php');

  13. exit;

  14. }

  15.  
  16. $result = $select->fetch_assoc();

  17. $gname = $result['gname'];

  18.  
  19. // 查询为空返回,避免空商品加到购物车

  20. if (empty($gname)) {

  21. header('location:shop.php');

  22. exit;

  23. }

  24.  
  25. addCart($gid, $gname);

  26.  
  27. //setcookie('cart','',time()-3600);

  28. header('location:shop.php');

showcar.php 购物车展示页面

 
  1. <?php

  2. require_once 'function.php';

  3.  
  4. ini_set('error_reporting', 'E_ALL & ~E_NOTICE');

  5. $shop_car = unserialize($_COOKIE['cart']);

  6. echo '<h1>购物车</h1>';

  7.  
  8. if (empty($shop_car)) {

  9. echo '购物车为空';

  10. echo '<br /><a href="shop.php">返回首页购物</a>';

  11. exit;

  12. }

  13.  
  14. // 展示

  15. $key = array_keys($shop_car);

  16. for ($i = 0; $i < count($shop_car); $i++) {

  17. echo '商品:' . $shop_car[$key[$i]][0] . ' 数量:' . $shop_car[$key[$i]][1] . '<br />';

  18. echo '<a href="showcar.php?del=' . $key[$i] . '">删除商品</a><br />';

  19. }

  20.  
  21. // 总价格

  22. echo '<br />总价格:$' . totalPrice($shop_car);

  23. if (isset($_GET['del'])) {

  24. $del = $_GET['del'];

  25. if ($_GET['del'] == $del) {

  26. delCart($shop_car, $del);

  27. }

  28. }

  29.  
  30. // 清空按钮

  31. echo '<br /><a href="showcar.php?clean=all">清空购物车</a><br />';

  32. if (isset($_GET['clean'])) {

  33. if ($_GET['clean'] == 'all') {

  34. delAll();

  35. }

  36. }

  37. ```

  38.  
  39. conn.php

  40. ```

  41. <?php

  42. define('host','localhost');

  43. define('user','root');

  44. define('pwd','');

  45. define('dbname','goods');

conn.php

 
  1. <?php

  2. define('host','localhost');

  3. define('user','root');

  4. define('pwd','');

  5. define('dbname','goods');

mysqli.php

 
  1. <?php

  2. include 'conn.php';

  3. $db = new mysqli(host,user,pwd,dbname);

猜你喜欢

转载自blog.csdn.net/sd19871122/article/details/81939740