php 購物車類實現代碼一例

來源:互聯網
上載者:User
  1. // 購物車類

  2. /*
  3. 使用說明:
  4. 建構函式 cart 可以使用參數:
  5. cart($cartname = 'myCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '')
  6. $cartname 是購物車的標識,可以指定,可以保證不重名,不會有相關衝突
  7. $session_id 是 session_id,預設是使用 cookie 來傳輸,也可以自訂,如果儲存類型是 session 才起效
  8. $savetype 儲存類型,有 session 和 cookie 方式
  9. ... 其他是 cookie 需要的參數

  10. 當程式本身用到session時,建議將此php購物車類改為cookie方式實現。

  11. //添加一個商品

  12. // 引用類
  13. require_once './cart.class.php';
  14. // 建立類執行個體
  15. $cart = new cart();

  16. // 商品已經存在 修改資料

  17. if ($cart->data[$id]) {
  18. $cart->data[$id]['count'] += $count;
  19. $cart->data[$id]['money'] += $cart->data[$id]['price'] * $count;
  20. // 添加商品
  21. } else {
  22. $cart->data[$id]['name'] = $name;
  23. $cart->data[$id]['price'] = $price;
  24. $cart->data[$id]['count'] = $count;
  25. $cart->data[$id]['money'] = $price * $count;
  26. }
  27. // 儲存購物車資料
  28. $cart->save();

  29. 編輯一個商品數量

  30. // 引用類
  31. require_once './cart.class.php';
  32. // 建立類執行個體
  33. $cart = new cart();

  34. // 商品已經存在 修改資料

  35. if ($cart->data[$id]) {
  36. $cart->data[$id]['count'] = $count;
  37. $cart->data[$id]['money'] = $cart->data[$id]['price'] * $count;

  38. // 儲存購物車資料

  39. $cart->save();
  40. }

  41. 刪除一個商品

  42. // 引用類
  43. require_once './cart.class.php';
  44. // 建立類執行個體
  45. $cart = new cart();

  46. // 刪除商品

  47. unset($cart->data[$id]);

  48. // 儲存購物車資料

  49. $cart->save();

  50. 列表購物車

  51. // 引用類
  52. require_once './cart.class.php';
  53. // 建立類執行個體
  54. $cart = new cart();

  55. foreach ($cart->data AS $k => $v) {

  56. echo '商品 ID: '.$k;
  57. echo '商品名稱: '.$v['name'];
  58. echo '商品單價: '.$v['price'];
  59. echo '商品數量: '.$v['count'];
  60. echo '商品總價: '.$v['money'];
  61. }

  62. 某欄位總累計 --- 如所有商品總價格

  63. // 引用類
  64. require_once './cart.class.php';
  65. // 建立類執行個體
  66. $cart = new cart();

  67. // 累計 money 欄位

  68. $cart->sum('money')

  69. 清空購物車

  70. // 引用類
  71. require_once './cart.class.php';
  72. // 建立類執行個體
  73. $cart = new cart();

  74. // 清除資料

  75. unset($cart->data);

  76. // 儲存購物車資料

  77. $cart->save();
  78. */

  79. //購物車類

  80. //edit bbs.it-home.org
  81. class cart {

  82. // 購物車標識

  83. var $cartname = '';
  84. // 儲存類型
  85. var $savetype = '';
  86. // 購物車中商品資料
  87. var $data = array();
  88. // Cookie 資料
  89. var $cookietime = 0;
  90. var $cookiepath = '/';
  91. var $cookiedomain = '';

  92. // 建構函式 (購物車標識, $session_id, 儲存類型(session或cookie), 預設是一天時間, $cookiepath, $cookiedomain)

  93. function cart($cartname = 'myCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '') {

  94. // 採用 session 儲存

  95. if ($savetype == 'session') {

  96. if (!$session_id && $_COOKIE[$cartname.'_session_id']) {

  97. session_id($_COOKIE[$cartname.'_session_id']);
  98. } elseif($session_id)
  99. session_id($session_id);

  100. session_start();

  101. if (!$session_id && !$_COOKIE[$cartname.'_session_id'])

  102. setcookie($cartname.'_session_id', session_id(), $cookietime + time(), $cookiepath, $cookiedomain);
  103. }

  104. $this->cartname = $cartname;

  105. $this->savetype = $savetype;
  106. $this->cookietime = $cookietime;
  107. $this->cookiepath = $cookiepath;
  108. $this->cookiedomain = $cookiedomain;
  109. $this->readdata();
  110. }

  111. // 讀取資料

  112. function readdata() {
  113. if ($this->savetype == 'session') {
  114. if ($_SESSION[$this->cartname] && is_array($_SESSION[$this->cartname]))
  115. $this->data = $_SESSION[$this->cartname];
  116. else
  117. $this->data = array();
  118. } elseif ($this->savetype == 'cookie') {
  119. if ($_COOKIE[$this->cartname])
  120. $this->data = unserialize($_COOKIE[$this->cartname]);
  121. else
  122. $this->data = array();
  123. }
  124. }

  125. // 儲存購物車資料

  126. function save() {
  127. if ($this->savetype == 'session') {
  128. $_SESSION[$this->cartname] = $this->data;
  129. } elseif ($this->savetype == 'cookie') {
  130. if ($this->data)
  131. setcookie($this->cartname, serialize($this->data), $this->cookietime + time(), $this->cookiepath, $this->cookiedomain);
  132. }
  133. }

  134. // 返回商品某欄位累加

  135. function sum($field) {

  136. $sum = 0;

  137. if ($this->data)
  138. foreach ($this->data AS $v)
  139. if ($v[$field])
  140. $sum += $v[$field] + 0;

  141. return $sum;

  142. }
  143. }
  144. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.