麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)仿寫CodeIgniter的購物車類

2024-05-04 23:38:21
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php實現(xiàn)仿寫CodeIgniter的購物車類,較為詳細的分析了購物車的功能與具體實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php實現(xiàn)仿寫CodeIgniter的購物車類。分享給大家供大家參考。具體如下:

這里仿寫CodeIgniter的購物車類

購物車基本功能:

1) 將物品加入購物車

2) 從購物車中刪除物品

3) 更新購物車物品信息 【+1/-1】

4) 對購物車物品進行統(tǒng)計

1. 總項目

2. 總數(shù)量

3. 總金額

5) 對購物單項物品的數(shù)量及金額進行統(tǒng)計

6) 清空購物車

cart.php文件如下:

 

 
  1. <?php 
  2. /** 
  3. * 
  4. * @author quanshuidingdang 
  5. */ 
  6. class Cart { 
  7. //物品id及名稱規(guī)則,調(diào)試信息控制 
  8. private $product_id_rule = '/.a-z0-9-_'//小寫字母 | 數(shù)字 | ._- 
  9. private $product_name_rule = '/./:a-z0-9-_';//小寫字母 | 數(shù)字 | ._-: 
  10. private $debug = TRUE; 
  11. //購物車 
  12. private $_cart_contents = array(); 
  13. /** 
  14. * 構(gòu)造函數(shù) 
  15. * 
  16. * @param array 
  17. */ 
  18. public function __construct() { 
  19. //是否第一次使用? 
  20. if(isset($_SESSION['cart_contents'])) { 
  21. $this->_cart_contents = $_SESSION['cart_contents']; 
  22. else { 
  23. $this->_cart_contents['cart_total'] = 0; 
  24. $this->_cart_contents['total_items'] = 0; 
  25. if($this->debug === TRUE) { 
  26. //$this->_log("cart_create_success"); 
  27. /** 
  28. * 將物品加入購物車 
  29. * 
  30. * @access public 
  31. * @param array 一維或多維數(shù)組,必須包含鍵值名:  
  32. id -> 物品ID標(biāo)識,  
  33. qty -> 數(shù)量(quantity),  
  34. price -> 單價(price),  
  35. name -> 物品姓名 
  36. * @return bool 
  37. */ 
  38. public function insert($items = array()) { 
  39. //輸入物品參數(shù)異常 
  40. if( ! is_array($items) OR count($items) == 0) { 
  41. if($this->debug === TRUE) { 
  42. $this->_log("cart_no_items_insert"); 
  43. return FALSE; 
  44. //物品參數(shù)處理 
  45. $save_cart = FALSE; 
  46. if(isset($items['id'])) { 
  47. if($this->_insert($items) === TRUE) { 
  48. $save_cart = TRUE; 
  49. else { 
  50. foreach($items as $val) { 
  51. if(is_array($val) AND isset($val['id'])) { 
  52. if($this->_insert($val) == TRUE) { 
  53. $save_cart = TRUE; 
  54. //當(dāng)插入成功后保存數(shù)據(jù)到session 
  55. if($save_cart) { 
  56. $this->_save_cart(); 
  57. return TRUE; 
  58. return FALSE; 
  59. /** 
  60. * 更新購物車物品信息 
  61. * 
  62. * @access public 
  63. * @param array 
  64. * @return bool 
  65. */ 
  66. public function update($items = array()) { 
  67. //輸入物品參數(shù)異常 
  68. if( !is_array($items) OR count($items) == 0) { 
  69. if($this->debug === TRUE) { 
  70. $this->_log("cart_no_items_insert"); 
  71. return FALSE; 
  72. //物品參數(shù)處理 
  73. $save_cart = FALSE; 
  74. if(isset($items['rowid']) AND isset($items['qty'])) { 
  75. if($this->_update($items) === TRUE) { 
  76. $save_cart = TRUE; 
  77. else { 
  78. foreach($items as $val) { 
  79. if(is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) { 
  80. if($this->_update($val) === TRUE) { 
  81. $save_cart = TRUE; 
  82. //當(dāng)更新成功后保存數(shù)據(jù)到session 
  83. if($save_cart) { 
  84. $this->_save_cart(); 
  85. return TRUE; 
  86. return FALSE; 
  87. /** 
  88. * 獲取購物車物品總金額 
  89. * 
  90. * @return int 
  91. */ 
  92. public function total() { 
  93. return $this->_cart_contents['cart_total']; 
  94. /** 
  95. * 獲取購物車物品種類 
  96. * 
  97. * @return int 
  98. */ 
  99. public function total_items() { 
  100. return $this->_cart_contents['total_items']; 
  101. /** 
  102. * 獲取購物車 
  103. * 
  104. * @return array 
  105. */ 
  106. public function contents() { 
  107. return $this->_cart_contents; 
  108. /** 
  109. * 獲取購物車物品options 
  110. * 
  111. * @param string 
  112. * @return array 
  113. */ 
  114. public function options($rowid = '') { 
  115. if($this->has_options($rowid)) { 
  116. return $this->_cart_contents[$rowid]['options']; 
  117. else { 
  118. return array(); 
  119. /** 
  120. * 清空購物車 
  121. * 
  122. */ 
  123. public function destroy() { 
  124. unset($this->_cart_contents); 
  125. $this->_cart_contents['cart_total'] = 0; 
  126. $this->_cart_contents['total_items'] = 0; 
  127. unset($_SESSION['cart_contents']); 
  128. /** 
  129. * 判斷購物車物品是否有options選項 
  130.  
  131. * @param string 
  132. * @return bool 
  133. */ 
  134. private function has_options($rowid = '') { 
  135. if( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) { 
  136. return FALSE; 
  137. return TRUE; 
  138. /** 
  139. * 插入數(shù)據(jù) 
  140. * 
  141. * @access private  
  142. * @param array 
  143. * @return bool 
  144. */ 
  145. private function _insert($items = array()) { 
  146. //輸入物品參數(shù)異常 
  147. if( ! is_array($items) OR count($items) == 0) { 
  148. if($this->debug === TRUE) { 
  149. $this->_log("cart_no_data_insert"); 
  150. return FALSE; 
  151. //如果物品參數(shù)無效(無id/qty/price/name) 
  152. if( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) { 
  153. if($this->debug === TRUE) { 
  154. $this->_log("cart_items_data_invalid"); 
  155. return FALSE; 
  156. //去除物品數(shù)量左零及非數(shù)字字符 
  157. $items['qty'] = trim(preg_replace('/([^0-9])/i''', $items['qty'])); 
  158. $items['qty'] = trim(preg_replace('/^([0]+)/i''', $items['qty'])); 
  159. //如果物品數(shù)量為0,或非數(shù)字,則我們對購物車不做任何處理! 
  160. if( ! is_numeric($items['qty']) OR $items['qty'] == 0) { 
  161. if($this->debug === TRUE) { 
  162. $this->_log("cart_items_data(qty)_invalid"); 
  163. return FALSE; 
  164. //物品ID正則判斷 
  165. if( ! preg_match('/^['.$this->product_id_rule.']+$/i', $items['id'])) { 
  166. if($this->debug === TRUE) { 
  167. $this->_log("cart_items_data(id)_invalid"); 
  168. return FALSE; 
  169. //物品名稱正則判斷 
  170. if( ! preg_match('/^['.$this->product_name_rule.']+$/i', $items['name'])) { 
  171. if($this->debug === TRUE) { 
  172. $this->_log("cart_items_data(name)_invalid"); 
  173. return FALSE; 
  174. //去除物品單價左零及非數(shù)字(帶小數(shù)點)字符 
  175. $items['price'] = trim(preg_replace('/([^0-9/.])/i''', $items['price'])); 
  176. $items['price'] = trim(preg_replace('/^([0]+)/i''', $items['price'])); 
  177. //如果物品單價非數(shù)字 
  178. if( ! is_numeric($items['price'])) { 
  179. if($this->debug === TRUE) { 
  180. $this->_log("cart_items_data(price)_invalid"); 
  181. return FALSE; 
  182. //生成物品的唯一id 
  183. if(isset($items['options']) AND count($items['options']) >0) { 
  184. $rowid = md5($items['id'].implode('', $items['options'])); 
  185. else { 
  186. $rowid = md5($items['id']); 
  187. //加入物品到購物車 
  188. unset($this->_cart_contents[$rowid]); 
  189. $this->_cart_contents[$rowid]['rowid'] = $rowid; 
  190. foreach($items as $key => $val) { 
  191. $this->_cart_contents[$rowid][$key] = $val; 
  192. return TRUE; 
  193. /** 
  194. * 更新購物車物品信息(私有) 
  195. * 
  196. * @access private 
  197. * @param array 
  198. * @return bool 
  199. */ 
  200. private function _update($items = array()) { 
  201. //輸入物品參數(shù)異常 
  202. if( ! isset($items['rowid']) OR ! isset($items['qty']) OR ! isset($this->_cart_contents[$items['rowid']])) { 
  203. if($this->debug == TRUE) { 
  204. $this->_log("cart_items_data_invalid"); 
  205. return FALSE; 
  206. //去除物品數(shù)量左零及非數(shù)字字符 
  207. $items['qty'] = preg_replace('/([^0-9])/i''', $items['qty']); 
  208. $items['qty'] = preg_replace('/^([0]+)/i''', $items['qty']); 
  209. //如果物品數(shù)量非數(shù)字,對購物車不做任何處理! 
  210. if( ! is_numeric($items['qty'])) { 
  211. if($this->debug === TRUE) { 
  212. $this->_log("cart_items_data(qty)_invalid"); 
  213. return FALSE; 
  214. //如果購物車物品數(shù)量與需要更新的物品數(shù)量一致,則不需要更新 
  215. if($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) { 
  216. if($this->debug === TRUE) { 
  217. $this->_log("cart_items_data(qty)_equal"); 
  218. return FALSE; 
  219. //如果需要更新的物品數(shù)量等于0,表示不需要這件物品,從購物車種清除 
  220. //否則修改購物車物品數(shù)量等于輸入的物品數(shù)量 
  221. if($items['qty'] == 0) { 
  222. unset($this->_cart_contents[$items['rowid']]); 
  223. else { 
  224. $this->_cart_contents[$items['rowid']]['qty'] = $items['qty']; 
  225. return TRUE; 
  226. /** 
  227. * 保存購物車數(shù)據(jù)到session 
  228.  
  229. * @access private 
  230. * @return bool 
  231. */ 
  232. private function _save_cart() { 
  233. //首先清除購物車總物品種類及總金額 
  234. unset($this->_cart_contents['total_items']); 
  235. unset($this->_cart_contents['cart_total']); 
  236. //然后遍歷數(shù)組統(tǒng)計物品種類及總金額 
  237. $total = 0; 
  238. foreach($this->_cart_contents as $key => $val) { 
  239. if( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) { 
  240. continue
  241. $total += ($val['price'] * $val['qty']); 
  242. //每種物品的總金額 
  243. $this->_cart_contents[$key]['subtotal'] = ($val['price'] * $val['qty']); 
  244. //設(shè)置購物車總物品種類及總金額 
  245. $this->_cart_contents['total_items'] = count($this->_cart_contents); 
  246. $this->_cart_contents['cart_total'] = $total; 
  247. //如果購物車的元素個數(shù)少于等于2,說明購物車為空 
  248. if(count($this->_cart_contents) <= 2) { 
  249. unset($_SESSION['cart_contents']); 
  250. return FALSE; 
  251. //保存購物車數(shù)據(jù)到session 
  252. $_SESSION['cart_contents'] = $this->_cart_contents; 
  253. return TRUE; 
  254. /** 
  255. * 日志記錄 
  256. * 
  257. * @access private 
  258. * @param string 
  259. * @return bool 
  260. */ 
  261. private function _log($msg) { 
  262. return @file_put_contents('cart_err.log', $msg, FILE_APPEND); 
  263. /*End of file cart.php*/ 
  264. /*Location /htdocs/cart.php*/ 

cart_demo.php文件如下:

 

 
  1. ?<?php 
  2. session_start(); 
  3. require_once('cart.php'); 
  4. $items = array( 
  5. 0 => array( 
  6. 'id' => 'sp001'
  7. 'qty' => 20, 
  8. 'price' => '10.50'
  9. 'name' => 'a002'
  10. 'options' => array( 
  11. 'made' => 'china'
  12. 'company' => 'bgi' 
  13. ), 
  14. 1 => array( 
  15. 'id' => 'sp002'
  16. 'qty' => 1, 
  17. 'price' => '3.50'
  18. 'name' => 'b002' 
  19. ); 
  20. $arr = array( 
  21. 'rowid' => '86dbb7cb58a667558b4bbb1f60330028'
  22. 'qty' => 21 
  23. ); 
  24. $cart = new Cart(); 
  25. $cart->insert($items); 
  26. //var_dump($cart->contents()); 
  27. $cart->update($arr); 
  28. var_dump($cart->contents()); 
  29. //$cart->destroy(); 
  30. //var_dump($_SESSION['cart_contents']); 
  31. /*end of php*/ 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 午夜亚洲视频 | teensexhd| av在线免费网| 一级视频网站 | 精品中文字幕视频 | 日日草夜夜 | av电影在线观看网址 | 黄污污网站 | 国产精品高潮99久久久久久久 | 中国大陆一级毛片 | 久久亚洲精品久久国产一区二区 | 免费观看9x视频网站在线观看 | 欧美69free性videos | 嫩嫩的freehdxxx | 毛片在线免费观看视频 | 亚洲国产精品久久久久久久久久 | 鲁丝片一区二区三区免费入口 | 毛片大全免费 | 成人一级免费 | 国产日韩一区二区三区在线观看 | 黄wwww| 久久国产精品无码网站 | 精品二区在线观看 | 国产女做a爱免费视频 | 91九色视频 | 久久91久久久久麻豆精品 | 久久久久久久久久久久久久av | 中文欧美日韩 | 少妇一级淫片免费放播放 | 九九精品在线观看视频 | 国产亚洲网 | 91 视频网站 | 宅男噜噜噜66国产在线观看 | 国产正在播放 | 91网站在线播放 | 国产一级aaa全黄毛片 | 日韩欧美色综合 | 久久爽久久爽久久av东京爽 | 欧美一级免费在线观看 | 久草在线最新免费 | 91精品国产乱码久 |