?
Example 1: $ SEL = $ THISCART-> Find ("Apple", "Name"); $ SEL = $ thiscart-> Find (2); // This sentence is the second product rations for the keyword value 2 The parameter will automatically match the key of the setKey method 2: $ SCONTENT = Array ("Apple", 12.5); $ SKEY = Array ("Name", "Gold"); $ SEL = $ THISCART-> FIND ($ Scontent, $ SKEY); // This example will look for the name Apple, the price of 12.5 // Clear Shop $ thiscart-> Clear (); // Reset Error message $ thiscart-> clearr (); / / ---------------------- Class attribute // Products $ thiscart-> sp_count; // Commodity total amount $ thiscart-> sp_sumgold; // Products Attribute array (custom column name, design) $ thiscart-> fields; // Product attribute number (custom column number, design) $ thiscart-> f_num; // Judgment Repeat keyword (custom, Time to design) $ thiscart-> f_key; // 金额 额 字 (列名, design) $ thiscart-> f_goldkey; // quantity keyword (column name, design) $ thiscart-> f_numkey; // Error message $ thiscart-> errMessage; // The middle of the country is returned to false, then this property stores error condition information // Product information array this property is a two-dimensional array, the first dimension is the product number, the second dimension is self Definition column name $ THISCART-> CART [$ xh] [$ fieldname]; // If you get the product name of the product 2 item: $ spname = $ thiscart-> Cart [2] ["name"]; --- -------------------------------------------------- ------------ * / Class TWT_CART {// class attribute ****************************** ******************************** VAR $ sp_count; // Product Class VAR $ S_SUMGOLD; // Product Total Cost Var $ fields; // Custom column var $ f_num; // Custom column number var $ f_key; / / as a keyword VAR that determines if the product is repeated $ F_GOLDKEY; // Products Field Name Used for product statistics calculation with var $ f_numkey; // Product Quantity Field Name Used in VAR $ ErrMessage; // Error Information
Var $ cart; // Constructor function TWT_CART () {// read some parameters // session_start (); ifcart ("))) session_register (" mycart "); $ mycart = $ _Session ["mycart"]; $ this-> sp_count = count ($ mycart); if (! Session_is_registered ("mc_fields")) session_register ("mc_fields"); $ this-> fields = $ _ session ["mc_fields"]; $ this-> f_num = count ($ THIS-> Fields) -1; if (! session_is_register ("mc_key")) session_register ("mc_key"); $ this-> f_key = $ _ session ["mc_key"]; if ( ! session_is_registered ( "mc_goldkey")) session_register ( "mc_goldkey"); $ this-> f_goldkey = $ _ SESSION [ "mc_goldkey"]; if (session_is_registered ( "mc_numkey"!)) session_register ( "mc_numkey"); $ this-> F_numkey = $ _ session ["mc_numkey"]; $ this-> CART = $ MyCart; $ this-> errmessage = "; $ this-> updatecart ();} // class method ********* *********************************************************** **** // Update total amount function updatecart () {// session_start (); if (! Session_is_register ("mycart")) {$ this-> errmessage = "session initialization error!"; Return false;} $ mycart = $ _Session ["mycart"]; $ TMP = 0; for ($ I = 0; $ I-> sp_count; $ i ) $ TMP = $ Mycart [$ I] [$ this-> f_goldkey] * $ MyCart [$ I] [$ this-> f_numkey]; $ this-> sp_sumgold = $ TMP;} // Setting custom field and initialization class parameters are array, the subscript is started with 0 Function SetField ($ TFIELD) {IF (is_Array ($ TFIELD)) {$ this-> Fields = $ tfield; // Initialize some of $ this-> f_num = count ($ tfield) -1; $ this-> f_key = $ tfield [0]; $ this-> F_Goldkey = $ tfield [0]; $ this-> f_numkey = $ tfield [0]; // session_start ();
$ _SESSION ["mc_fields"] = $ tfield; $ _SESSION ["mc_key"] = $ TFIELD [0]; $ _SESSION ["mc_goldkey"] = $ tfield [0]; $ _SESSION ["mc_numkey"] = $ TFIELD [ 0];} else {$ this-> errMessage = "parameter transfer error, please enter the array of subscripts from 0!"; Return false;}} // setfield End
// Set keyword (unique keyword, like id, quantity column name,) Function SetKey ($ key, $ numkey, $ goldkey) {$ this-> f_key = $ key; $ this-> f_goldkey = $ = $-> f_goldkey = $ GoldKey; $ this-> f_numkey = $ numkey; // session_start (); $ _SESSION ["mc_key"] = $ key; $ _SESSION ["mc_goldkey"] = $ goldKey; $ _SESSION ["mc_numkey] = $ numkey; } // Clear error message Function clearrr () {$ this-> errMessage = "";} // Clear Shopping basket Function clear () {$ mycart = $ _ session ["mycart"]; unset ($ mycart); $ _SESSION [ "mycart"] = $ mycart; session_unregister ( "mycart"); session_unregister ( "mc_fields"); session_unregister ( "mc_key"); session_unregister ( "mc_goldkey"); session_unregister ( "mc_numkey"); $ this-> cart = $ MyCart; $ this-> sp_count = 0; $ this-> sp_sumgold = 0;} // Functional method / / * Find something item, give a lookup content or content array lookup field name or field name group return product number array Can't find a false to confirm that the lookup key column If the parameter is an array, then the content array and field name must correspond to, and the number is equal * / Function Find ($ SSTR, $ Sfield = "0") {// session_start ( $ SKEY = Array (); $ sfStr = array (); $ mycart = $ _ session ["mycart"]; // Unified parameter IF (is_ARRAY ($ sstr)) {if (! Is_ARRAY ($ sfield)) {$ this-> errmessage = "parameter type is inconsistent! "} $ SKEY = $ sfield; $ sfstr = $ sstr;} else {if (is_ARRAY ($ SFIELD)) {$ this-> errmessage =" parameter type is inconsistent! "}} ($ Sfield ==" 0 ") $ skey [0] = $ this-> f_key; else $ skey [0] = $ sfield; $ sfstr [0] = $ sstr;} $ r }r = Array (); // Find $ CNT = 0; for ($ I = 0; $ I <$-> sp_count; $ i ) {$ TFLAG = true;
// Array keyword and content to find for for ($ j = 0; $ j ) IF ($ Mycart [$ I] [$ SKEY [$ j]]! = $ Sfstr [$ J]) $ tflag = false; if ($ tflag) $ RTARRAY [$ CNT ] = $ I;} Return $ RTARRAY;} // Add goods array parameters, named the subscript Function Addone with custom field ($ SPLIST ) {IF ($ SPLIST)) || (($ SPLIST) -1)! = $ This-> errmessage = "parameter transfer error, please enter Define the array of field names and a number of matches! "; Return false;} // session_start (); $ mycart = $ _ session [" mycart "]; // Determine if the product already exists, if present, Plus $ tflag = true; for ($ I = 0; $ i <$-> sp_count; $ i ) {IF ($ MyCart [$ I] [$ this-> f_key] == $ SPLIST [$ THIS- > f_key]) {$ tflag = false; // Add product number $ MyCart [$ I] [$ this-> f_numkey] = $ SPLIST [$ this-> f_numkey]; $ i = $ this-> sp_count; }} // Add item to the list IF ($ TFLAG) {for ($ I = 0; $ I <= $ this-> f_num; $ i ) $ mycart [$ this-> sp_count] [$ this-> fields [$ I]] = $ SPLIST [$ this-> Fields [$ I]]; $ this-> sp_count = 1;} $ this-> Cart = $ mycart; $ _SESSION ["Mycart"] = $ mycart; $ THIS-> UpdateCart ();} // add end // Delete a commodity parameter is the order in the shopping cart Function Delone ($ ID) {// session_start (); $ mycart = $ _SESSION ["MyCart"]; for ($ I = $ ID; $ I-> sp_count-1; $ i ) {for $ J = 0; $ j <= ) $ mycart [$ I] [$ THIS-> Fields [$ j] = $ mycart [$ i 1] [$ this-> Fields [$ j]];} unset ($ mycart [$ this-> sp_count-1]); $ this-> sp_count- = 1; $ _SESSION ["Mycart"] = $ mycart; $ this-> cart = $ mycart ; $ This->
UpdateCart ();} // del end // Modify the quantity parameter of a commodity for the item in the shopping cart Need to change the number of Function ModifyOne ($ ID, $ NUM) {// session_start (); $ mycart = $ _Session ["mycart"]; if (! EMPTY ($ Mycart [$ ID] [$ this-> f_key]) {= $ mycart [$ ID] [$ this-> f_numkey] = $ Num; $ this-> CART = $ MyCart; $ _SESSION ["Mycart"] = $ mycart; $ this-> Updatecart ();} else {$ this-> errMessage = "The number of items is less than or equal to zero!"; Return False;}} else {$ this-> errMessage = "Products do not exist!"; Return False;}} // Modify a unit price parameter of a commodity introduction with the number of modified items Function ModifyPrice ($ ID, $ Num) {//// Session_start (); $ mycart = $ _SESSION ["mycart"]; if (! EMPTY ($ mycart [$ ID] [$ this-> f_key]) {f ($ num> 0) {$ mycart [$ ID] [$ this-> f_goldkey] = $ num; $ this-> cart = $ mycart; $ _SESSION ["mycart"] = $ mycart; $ this-> updatecart ();} else {$ this-> errmessage = "Product The amount is less than or equal to zero! "; Return False;}} else {$ this-> errmessage =" Product does not exist! "; Return false;}} // modifyprice end // Some items plus 1 parameter is the serial number of the item in the shopping cart Function Add1 ($ ID) {// session_start (); $ mycart = $ _SESSION ["mycart"]; $ Mycart [$ ID] [$ this-> f_numkey] = 1; $ this-> Cart = $ mycart; $ _SESSION ["Mycart"] = $ mycart; $ this-> updatecart ();} // Some items Decrease 1 parameter is the serial number of the item in the shopping cart Function Del1 ($ ID) {// session_start (); $ mycart = $ _SESSION ["mycart"]; if ($ mycart [$ ID] [$ this-> f_numkey] > 1) $ mycart [$ ID] [$ this-> f_numkey] - = 1;