This article mainly introduces the shopping cart class implemented by PHP, which can be used to add, delete, and collect statistics of shopping cart, for more information about the shopping cart class implemented by PHP, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
This shopping cart class is implemented based on the shopping cart category of CodeIgniter.
The basic functions of a shopping cart are as follows:
1) add an item to the shopping cart
2) delete an item from the shopping cart
3) update the shopping cart item information (+ 1/-1]
4) make statistics on shopping cart items
1. total projects
2. total quantity
3. total amount
5) count the quantity and amount of individual shopping items
6) clear the shopping cart
1. cart. php file:
Signature <? Php/***** @ author quanshuidingdang */class Cart {// item id and name rules, debugging information control private $ product_id_rule = '\. a-z0-9-_ '; // lowercase letter | number |. _-private $ product_name_rule = '\. \: a-z0-9-_ '; // lowercase letter | number |. _-: private $ debug = TRUE; // shopping carprivate $ _ cart_contents = array (); /*** constructor *** @ param array */public function _ construct () {// is it used for the first time? 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 ");}} /*** add an item to the shopping cart ** @ access public * @ param array one-dimensional or multi-dimensional array, which must contain the key value name: id-> item ID identifier, qty-> quantity (quantity), price-> unit price (price), name-> item name * @ 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 processing $ 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 ;}}// after successful insertion, save the data to the session If ($ save_cart) {$ this-> _ save_cart (); return TRUE;} return FALSE ;} /*** update The cart item information ** @ access public * @ param array * @ return bool */public function update ($ 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 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 ($ val) = TR UE) {$ save_cart = TRUE ;}}// when the update is successful, save the data to the session if ($ save_cart) {$ this-> _ save_cart (); return TRUE;} return FALSE;}/*** get the total amount of items in the shopping cart *** @ return int */public function total () {return $ this-> _ cart_contents ['cart _ total'];}/*** get the cart item category ** @ return int */public function total_items () {return $ this-> _ cart_contents ['total _ items '];}/*** get shopping cart ** @ return array */public function conten Ts () {return $ this-> _ cart_contents ;} /*** get the options of the shopping cart *** @ param string * @ return array */public function options ($ rowid = '') {if ($ this-> has_options ($ rowid) {return $ this-> _ cart_contents [$ rowid] ['options'];} else {return array () ;}/ *** clear shopping 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 whether an item in the shopping cart has the options option ** @ 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 ;} /*** 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 the item parameter is invalid (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;} // removes the number of items from the left zero and non-numeric characters $ items ['qty '] = trim (preg_replace ('/([^ 0-9]) /I ', '', $ items ['qty']); $ items ['qty '] = trim (preg_replace ('/^ ([0] +) /I ', '', $ items ['qty']); // if the number of items is 0 or not a number, we will not process the shopping cart! If (! Is_numeric ($ items ['qty ']) OR $ items ['qty'] = 0) {if ($ this-> debug = TRUE) {$ this-> _ log ("cart_items_data (qty) _ invalid");} return FALSE;} // item ID regular judgment if (! Preg_match ('/^ ['. $ this-> product_id_rule. '] + $/I', $ items ['id']) {if ($ this-> debug = TRUE) {$ this-> _ log ("cart_items_data (id) _ invalid");} return FALSE;} // The Item Name regular judgment if (! Preg_match ('/^ ['. $ this-> product_name_rule. '] + $/I', $ items ['name']) {if ($ this-> debug = TRUE) {$ this-> _ log ("cart_items_data (name) _ invalid");} return FALSE;} // remove the left zero or non-numeric unit price of an item (with decimal point) character $ items ['price'] = trim (preg_replace ('/([^ 0-9 \.]) /I ', '', $ items ['price']); $ items ['price'] = trim (preg_replace ('/^ ([0] +) /I ', '', $ items ['price']); // if the unit price of an item is not a number if (! Is_numeric ($ items ['price']) {if ($ this-> debug = TRUE) {$ this-> _ log ("cart_items_data (price) _ invalid ");} return FALSE;} // generates the unique id of an item if (isset ($ items ['options']) AND count ($ items ['options'])> 0) {$ rowid = md5 ($ items ['id']. implode ('', $ items ['options']);} else {$ rowid = md5 ($ items ['id']);} // add an item to the shopping cart unset ($ this-> _ cart_contents [$ rowid]); $ this-> _ cart_contents [$ rowid] ['rowid'] = $ rowid; forea Ch ($ items as $ key =>$ val) {$ this-> _ cart_contents [$ rowid] [$ key] = $ val;} return TRUE ;} /*** update The cart item 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 left zero and non-numeric characters of the number of items $ items ['qty '] = preg_replace ('/([^ 0-9])/I ','', $ items ['qty ']); $ items ['qty'] = preg_replace ('/^ ([0] +)/I ','', $ items ['qty ']); // if the number of items is not a number, the shopping cart will not be processed! If (! Is_numeric ($ items ['qty ']) {if ($ this-> debug === TRUE) {$ this-> _ log ("cart_items_data (qty) _ invalid ") ;}return FALSE ;}// if the number of items in the shopping cart is the same as the number of items to be updated, if ($ this-> _ cart_contents [$ items ['rowid'] ['qty '] = $ items ['qty']) {if ($ this-> debug === TRUE) {$ this-> _ log ("cart_items_data (qty) _ equal") ;}return FALSE ;} // if the number of items to be updated is equal to 0, this item is not required, clear from the shopping cart type // otherwise, modify the number of items in the shopping cart to be equal to the number of items entered. if ($ items ['qty '] = 0) {unset ($ t His-> _ cart_contents [$ items ['rowid']);} else {$ this-> _ cart_contents [$ items ['rowid'] ['qty '] = $ items ['qty'];} return TRUE ;} /*** save the shopping cart data to the session ** @ access private * @ return bool */private function _ save_cart () {// first clear the total item categories and total amount of the shopping cart unset ($ this-> _ cart_contents ['total _ items ']); unset ($ this-> _ cart_contents ['cart _ total']); // then traverse the array to calculate the item type and 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']); // total amount of each item $ this-> _ cart_contents [$ key] ['subtotal'] = ($ val ['price'] * $ val ['qty ']);} // set the total item category and total amount of the shopping cart $ this-> _ cart_contents ['total _ items '] = count ($ this-> _ cart_contents ); $ this-> _ cart_contents ['cart _ total'] = $ total; // if the number of elements in a shopping cart is less than or equal to 2, if (count ($ this-> _ cart_contents) <= 2) {unset ($ _ SESSION ['cart _ contents']); return FALSE ;} // Save the shopping 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 file is as follows:
<?phpsession_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*/
I hope this article will help you with php programming.