萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 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;
copyright © 萬盛學電腦網 all rights reserved