Examples of shopping cart classes implemented in PHP

Source: Internet
Author: User
Tags foreach bool numeric md5 trim unique id

This article illustrates the shopping cart class implemented by PHP. Share to everyone for your reference. The specific analysis is as follows:

The shopping cart class is based on the codeigniter of the shopping cart class.

The basic function of shopping cart is as follows:

1 Add items to the shopping cart
2 Remove items from the shopping cart
3 Update Shopping Cart items Information "+1/-1"
4 The shopping cart items for statistics
1. General Projects
2. Total Quantity
3. Total Amount
5 The number of items and the amount of shopping item statistics
6) Empty the shopping cart

1. cart.php Documents:

?
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 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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-1 41 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-2 02 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-2 63 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 {//item ID and name rules, debug information control private $product _id_rule = '. A-z0-9-_ ';//Small Write Letters | number |. _-private $product _name_rule = '.: A-z0-9-_ ';//Small Letter | number |. _-: Private $debug = TRUE; Shopping cart Private $_cart_contents = array (); /** * Constructor * * @param array/Public function __construct () {//is used for the first time? if (Isset ($_session[' cart_contents ')) {$this-&G t;_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");}} /** * Add Items to Cart * * @access public * @param array one-dimensional or multidimensional array, must contain the key value name: ID-> item ID identification, QTY-> Quantity (quantity), Price-> ASP ( Price), name-> item names * @return BOOL */Public Function Insert ($items = Array ()) {//input item parameter exception if (! Is_array ($items) OR Count ($items) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Cart_no_items_insert");} return FALSE;}//Item parameter handling $s Ave_cart = FALSE; if (Isset ($items [' id ']) {if ($this->_insert ($items) = = True) {$save _cart = TRUE;}} else {foreach ($items as $val) {if Is_ar Ray ($val) and Isset ($val [' ID '])) {if ($this->_insert ($val) = = True) {$save _cart = True;}}} ///When the insert succeeds save the data to session if ($save _cart) {$this->_save_cart (), return TRUE; /** * Update Shopping cart Article information * @access public * @param array * @return BOOL/Public Function update ($items = Array ()) {//input item parameter vary Constant if (!is_array ($items) OR count ($items) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Cart_no_items_insert");} return FALSE; }//Item parameter processing $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 ($va L) = = = True) {$save _cart = true;}} If the update succeeds, save the data to session if ($save _cart) {$this->_save_cart (), return TRUE, return FALSE; /** * Get the total amount of shopping cart items ** @return Int/Public Function total () {return $this->_cart_contents[' cart_total '];}/** * Get cart Item Type * * @return in T/Public Function Total_items () {return $this->_cart_contents[' Total_items '];/** * Get cart * * @return Array/PU Blic function contents () {return $this->_cart_contents;}/** * Get shopping cart items Options * * @param string * @return Array/PU Blic function Options ($rowid = ') {if ($this->has_options ($rowid)) {return $this->_cart_contents[$rowid] [' Options ']; else {return array ();}} /** * Empty cart */Public function destroy () {unset ($this->_cart_contents); $this->_cart_contents[' cart_total '] = 0; $this->_cart_contents[' total_items '] = 0; unset ($_session[' cart_contents ')); /** * Determine if shopping cart items have options option * * @param string * @return bool/Private Function has_options ($rowid = ") {if (! Isset) ($t his->_cart_contents[$rowid] [' Options ']] OR count ($this->_cart_contents[$rowid [' options ']) = = 0) {return FALSE; return TRUE; /** * Insert Data * * @accESS Private * @param array * @return BOOL/Private Function _insert ($items = Array ()) {//input item parameter exception if (! Is_array ($items ) OR count ($items) = = 0) {if ($this->debug = = TRUE) {$this->_log ("Cart_no_data_insert");} return FALSE;///If Item parameters Invalid number (no 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;///Remove item quantity left 0 and non-digit character $ items[' qty '] = Trim (Preg_replace ('/([^0-9])/I ', ', $items [' qty '])); $items [' qty '] = Trim (Preg_replace ('/^ ([0]+)/I ', ', $items [' qty '])); If the item quantity is 0, or not the number, then we do not have to do any processing to the shopping cart! if (! is_numeric ($items [' qty ']) OR $items [' qty '] = = 0) {if ($this->debug = = TRUE) {$this->_log ("Cart_items_data" (qt y) _invalid "); return FALSE; }//item ID is judged if (! Preg_match ('/^['. $this->product_id_rule. ') +$/i ', $items [' id ']) {if ($this->debug = = TRUE) {$this->_log ("Cart_items_data (ID) _invalid"); The name of the item isThen judge if (! Preg_match ('/^['. $this->product_name_rule. ') +$/i ', $items [' name ']) {if ($this->debug = = TRUE) {$this->_log ("Cart_items_data (name) _invalid"); ; //Remove Item unit price left 0 and Non-numeric (with decimal points) character $items [' prices '] = Trim (Preg_replace ('/([^0-9.]) /I ', ', $items [' Price ']]); $items [' price '] = Trim (Preg_replace ('/^ ([0]+)/I ', ', $items [' Price ']); If the item unit price is not a digital if (! is_numeric ($items [' prices ']) {if ($this->debug = = TRUE) {$this->_log ("Cart_items_data") Invalid "); return FALSE; }//Generate unique ID if (isset ($items [' Options ']) and count ($items [' Options ']) >0 {$rowid = MD5 ($items [' ID '].implode (', $ items[' options ']); else {$rowid = MD5 ($items [' id ']);}//Add items to Cart unset ($this->_cart_contents[$rowid]); $this->_cart_contents[$rowid] [' rowid '] = $rowid; foreach ($items as $key => $val) {$this->_cart_contents[$rowid] [$key] = $val;} return TRUE; /** * Update Shopping Cart article information (private) * @access private * @param array * @return BOOL/Private Function _update ($items = Array ()) {//input Item parameter exception 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;} Remove the number of items left 0 and non-numeric $items [' qty '] = preg_replace ('/[[^0-9])/I ', ', $items [' qty ']); $items [' qty '] = preg_replace ('/^ ([0]+)/I ', ', $items [' qty ']); If the number of items is not digital, do not do any processing on the shopping cart! if (! is_numeric ($items [' qty ']) {if ($this->debug = = TRUE) {$this->_log ("Cart_items_data (qty) _invalid");} return FALSE; If the number of shopping cart items is consistent with the number of items that need to be updated, you do not need to update the IF ($this->_cart_contents[$items [' rowID ']][' qty '] = = $items [' qty ']) {if ($this- >debug = = TRUE) {$this->_log ("Cart_items_data (qty) _equal"); //If you need to update the number of items equal to 0, indicating that you do not need this item, from the shopping cart type to clear/or modify the number of shopping cart items equal to the number of items entered if ($items [' qty '] = = 0) {unset ($this->_cart_contents[$ items[' rowID ']); else {$this->_cart_contents[$items [' rowID ']][' qty '] = $items [' qty '];} return TRUE; /** * Save CART Data to Session * @access private * @return bool/privAte function _save_cart () {///First Clear Cart Total item type and total amount unset ($this->_cart_contents[' total_items '); unset ($this->_cart _contents[' cart_total ']); Then iterate over the array of categories of items and the total amount $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 ']); The total amount of each item $this->_cart_contents[$key] [' subtotal '] = ($val [' Price '] * $val [' qty ']); //Set the total item type and total amount of the cart $this->_cart_contents[' total_items '] = count ($this->_cart_contents); $this->_cart_contents[' cart_total '] = $total; If the number of elements in the shopping cart is less than 2, the shopping cart is empty if (count ($this->_cart_contents) <= 2) {unset ($_session[' cart_contents ')); FALSE; //Save CART Data to session $_session[' cart_contents ' = $this->_cart_contents; return TRUE; /** * Log Record * @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. The cart_demo.php document reads as follows:

?
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 ' =>, ' price ' => ' 10.50 ', ' name ' => ' a002 ', ' options ' = > Array (' Made ' => ', ' => ', ' Company ', ' BGI ')), 1 => array (' ID ' => ' sp002 ', ' qty ' => ', ' the ', ' ", ', '), ' Price =&G ' T ' 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*/

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.