程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP實現的購物車類實例

PHP實現的購物車類實例

編輯:關於PHP編程

     本文實例講述了PHP實現的購物車類。分享給大家供大家參考。具體分析如下:

    該購物車類是基於CodeIgniter的購物車類仿寫實現的。

    購物車基本功能如下:

    1) 將物品加入購物車
    2) 從購物車中刪除物品
    3) 更新購物車物品信息 【+1/-1】
    4) 對購物車物品進行統計
    1. 總項目
    2. 總數量
    3. 總金額
    5) 對購物單項物品的數量及金額進行統計
    6) 清空購物車

    1. cart.php文件:

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

    2. cart_demo.php文件如下:

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

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

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved