PHP shopping cart, transplanted to CodeIgniter

Source: Internet
Author: User
Tags codeigniter
PHP shopping cart class, transplanted to CodeIgniter & lt ;? Php *** shopping cart program ModifiedbyCodeIgniter *** classcart {perform regular validation on the product ID and product name attributes var $ product_id_rules.a-z0-9 _-; var $ product_name_ru PHP shopping cart class, transplanted to CodeIgniter

 Session ('cart _ contents ')! = FALSE) {$ this-> _ cart_contents = $ this-> session ('cart _ contents ');} else {// initialize data $ this-> _ cart_contents ['cart _ total'] = 0; $ this-> _ cart_contents ['total _ items '] = 0 ;}} // --------------------------------/*** ADD to shopping cart ** @ accesspublic * @ paramarray * @ returnbool */function insert ($ items = array ()) {// check whether the data is correct if (! Is_array ($ items) OR count ($ items) = 0) {return FALSE;} // you can add a commodity (one-dimensional array ), you can also add multiple items (two-dimensional array) $ 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 ;}}// update data if ($ save_cart = TRUE) {$ this-> _ save_cart (); return TRUE;} return FALSE ;} //---- --------------------------/*** Process data inserted into the shopping cart ** @ accessprivate * @ paramarray * @ returnbool */function _ insert ($ items = array () {// Check if (! Is_array ($ items) OR count ($ items) = 0) {return FALSE;} // --------------------------------/* The first four array indexes (id, qty, price, and name) is required. If one of them is missing, the data will not be saved to the shopping cart. 5th indexes (options) are optional. When your product contains related options, you can use it. Use an array to save the option information. Note: the value of $ data ['price'] must be greater than 0, for example, $ data = array ('id' => 'sku _ 123abc', 'qty '=> 1, 'price' => 39.95, 'name' => 't-shirt', 'options' => array ('size' => 'L ', 'color' => 'red'); */if (! Isset ($ items ['id']) OR! Isset ($ items ['qty ']) OR! Isset ($ items ['price']) OR! Isset ($ items ['name']) {return FALSE;} // -------------------------------- // verify the quantity, not to replace a number with null $ items ['qty '] = trim (preg_replace ('/([^ 0-9])/I ','', $ items ['qty ']); // quantity verification $ items ['qty'] = trim (preg_replace ('/(^ [0] +)/I ', '', $ items ['qty ']); // The number must be a number or not 0 if (! Is_numeric ($ items ['qty ']) OR $ items ['qty'] = 0) {return FALSE;} // -------------------------- // verify the product ID if (! Preg_match ("/^ [". $ this-> product_id_rules. "] + $/I", $ items ['id']) {return FALSE;} // -------------------------------- // verify the product name. Considering the Chinese characters, do not use/* if (! Preg_match ("/^ [". $ this-> product_name_rules. "] + $/I", $ items ['name']) {return FALSE ;} * // ---------------------------------- // price Verification $ items ['price'] = trim (preg_replace ('/([^ 0-9 \.]) /I ', '', $ items ['price']); $ items ['price'] = trim (preg_replace ('/(^ [0] +) /I ', '', $ items ['price']); // verify whether the price is a value if (! Is_numeric ($ items ['price']) {return FALSE;} // -------------------------------- // property verification. if the property exists, the property value + product ID is encrypted AND saved in $ rowid. if (isset ($ items ['options']) AND count ($ items ['options'])> 0) {$ rowid = md5 ($ items ['id']. implode ('', $ items ['options']);} else {// encrypt the product ID directly when no attribute exists $ rowid = md5 ($ items ['id']);} // check whether the product exists in the shopping cart. If yes, add the number of items added this time on the basis of the original $ _ contents = $ this-> _ cart_contents; $ _ tmp_contents = array (); foreach ($ _ cont Ents as $ val) {if (is_array ($ val) AND isset ($ val ['rowid']) AND isset ($ val ['qty ']) AND $ val ['rowid'] = $ rowid) {$ _ tmp_contents [$ val ['rowid'] ['qty '] = $ val ['qty'];} else {$ _ tmp_contents [$ val ['rowid'] ['qty '] = 0 ;}} // -------------------------------- // clear the original data unset ($ this-> _ cart_contents [$ rowid]); // assign a value to $ this-> _ cart_contents [$ rowid] ['rowid'] = $ rowid; // add a new project foreach ($ items as $ key => $ val) {if ($ key = 'Q Ty '& isset ($ _ tmp_contents [$ rowid] [$ key]) {$ this-> _ cart_contents [$ rowid] [$ key] = $ val + $ _ tmp_contents [$ rowid] [$ key];} else {$ this-> _ cart_contents [$ rowid] [$ key] = $ val ;}} return TRUE ;} // --------------------------------/*** update the shopping cart ** @ accesspublic * @ paramarray * @ paramstring * @ returnbool */function update ($ items = array ()) {// verify if (! Is_array ($ items) OR count ($ items) = 0) {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 ;}}}} if ($ save_cart = TRUE) {$ this-> _ save_cart (); return TRUE;} return FALSE;} // ------------------------------/*** process the updated shopping cart ** @ accessprivate * @ paramarray * @ returnbool */function _ update ($ items = array () {if (! Isset ($ items ['qty ']) OR! Isset ($ items ['rowid']) OR! Isset ($ this-> _ cart_contents [$ items ['rowid']) {return FALSE ;} // quantity detected $ items ['qty '] = preg_replace ('/([^ 0-9])/I ',', $ items ['qty ']); if (! Is_numeric ($ items ['qty ']) {return FALSE ;} if ($ this-> _ cart_contents [$ items ['rowid'] ['qty '] = $ items ['qty']) {return FALSE ;} if ($ items ['qty '] = 0) {unset ($ this-> _ cart_contents [$ items ['rowid']);} else {$ this-> _ cart_contents [$ items ['rowid'] ['qty '] = $ items ['qty'];} return TRUE ;} // --------------------------------/*** save the shopping cart to the Session ** @ accessprivate * @ returnbool */function _ save_cart () {unset ($ this-> _ Cart_contents ['total _ items ']); unset ($ this-> _ cart_contents ['cart _ total']); $ total = 0; $ items = 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']); $ items + = $ val ['qty ']; $ this-> _ cart_contents [$ key] ['subtotal'] = ($ this-> _ cart_contents [$ key] ['price'] * $ this-> _ cart_contents [$ key] ['qty ']);} $ this-> _ cart_contents ['total _ items '] = $ items; $ this-> _ cart_contents ['cart _ total'] = $ total; if (count ($ this-> _ cart_contents) <= 2) {$ this-> session ('cart _ Contents', array (); return FALSE;} $ thi S-> session ('cart _ Contents', $ this-> _ cart_contents); return TRUE ;} // --------------------------------/*** total amount in the shopping cart ** @ accesspublic * @ returninteger */function total () {return $ this-> _ cart_contents ['cart _ total'];} // --------------------------------/*** total number of items in the shopping cart *** @ accesspublic * @ returninteger */function total_items () {return $ this-> _ cart_contents ['total _ items '];} // ----------------------- -------/*** Array of all information in the shopping cart ** returns an array containing all information in the shopping cart ** @ accesspublic * @ returnarray */function contents () {$ cart = $ this-> _ cart_contents; unset ($ cart ['total _ items ']); unset ($ cart ['cart _ total']); return $ cart;} // ------------------------------/*** whether a specific column in the shopping cart contains option information ** if a specific column in the shopping cart contains option information, this function returns TRUE (Boolean). This function is designed to work with contents () use ** @ accesspublic * @ returnarray */function has_options ($ rowid = '') together in a loop {if (! Isset ($ this-> _ cart_contents [$ rowid] ['options']) OR count ($ this-> _ cart_contents [$ rowid] ['options']) ===0) {return FALSE;} return TRUE ;} // --------------------------------/*** return the option information of a specific product in the form of an array. ** This function is designed to be consistent with contents () use ** @ accesspublic * @ returnarray */function product_options ($ rowid = '') {if (! Isset ($ this-> _ cart_contents [$ rowid] ['options']) {return array ();} return $ this-> _ cart_contents [$ rowid] ['options'];} // --------------------------------/***** format the value of the value with the decimal point after formatting is returned (two digits after the decimal point ), the general price is ** @ accesspublic * @ returninteger */function format_number ($ n = '') {if ($ n ='') {return '';} $ n = trim (preg_replace ('/([^ 0-9 \.]) /I ', '', $ n); return number_format ($ n, 2 ,'. ',',');}//--------------------- -----------/*** Destroy shopping cart ** This function is generally called after processing the user's order ** @ accesspublic * @ returnnull */function destroy () {unset ($ this-> _ cart_contents); $ this-> _ cart_contents ['cart _ total'] = 0; $ this-> _ cart_contents ['total _ items '] = 0; $ this-> session ('cart _ Contents', array ());} // --------------------------------/*** save the Session ** must have session_start (); ** @ accessprivate * @ returnbool */function session ($ name = 'cart _ contents', $ Value = NULL) {if ($ name = '') $ name = 'cart _ contents '; if ($ value = NULL) {return @ $ _ SESSION [$ name];} else {if (! Empty ($ value) & is_array ($ value) {$ _ SESSION [$ name] = $ value; return TRUE ;}else {return FALSE ;}}}?>

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.