Modified PHP shopping car

xiaoxiao2021-03-06  106

When using Smarty to call the shopping cart, it was found that it could not be successfully displayed. After a day, I finally understood. Below is a modified shopping car

/ ** * cart * * Shopping car * * @Author doodoo * @package cart * @category cart * @License php license * @access public * @version $ revision: 1.20 $ * @date 2004-08-21 * / class cart {

Var $ cart; var $ totalcount; // Total number of products VAR $ TOTALPRICES; // Commodity total amount

/ ** * CART Constructor * * Class constructor so that the shopping cart maintains a stable initialization status * * @Static * @access public * @Return Void free * @Param void no parameters * / function cart () {$ This-> totalcount = 0; $ this-> Totalprice = 0; $ this-> cart = array ();} //}}} // {{{add ($ item)

/ ** * Add goods to current shopping cart * * @access public * @Param Array $ item product information (1D number: Array (product ID, product name, commodity single price, product quantity) * @Return Array Return to current shopping The array of car goods * / function add ($ it) {if (! Is_ARRAY ($ item) | i_null ($ item)) Return $ this -> _ getArray ($ this-> cart); if (! Is_numeric (End) ($ item)) || ($ ITEM))))))) {echo "price and quantity must be a number"; return $ this -> _ getArray ($ this-> Cart);} reset ($ item) // This sentence is necessary, because the above judgment has moved the array of indicators $ key = current ($ item); if ($ key == ") Return $ this -> _ getArray ($ this-> cart) ; If ($ this -> _ ISEXISTS ($ key)) {// Is there any existence? $ This-> Cart [$ key] ['count'] = end ($ item); Return $ this -> _ getArray ($ this-> cart);}

$ this-> CART [$ key] = $ key; $ this-> cart [$ key] ['name'] = next ($ it); $ this-> cart [$ key] [' Price '] = next ($ item); $ this-> Cart [$ key] [' count '] = next ($ item);

Return $ this -> _ getArray ($ this-> cart);

//}}} // {{{add ($ it)

/ ** * From the current shopping cart * When $ key == "", empty the current shopping cart * When $ key! = "&& $ count ==" ", from the current shopping All products of the product ID number are $ Key * When $ key! = "&& $ count! =" ", You pick $ COUNT ID number from the current shopping cart * * * @access public * @Param string $ key Product ID * @Return Mixed Returns the array of true vacations or current shopping cart * / Function Remove ($ key = ", $ count =" ") {if ($ key = = "" "{$ This-> Cart = array (); return true;} f (! Array_key_exists ($ key, $ this-> cart) Return False; if ($ count ==") {// shift Go to this class unset ($ this-> CART [$ key]);} else {// Remove $ COUNT $ 10-> Cart [$ key] ['count'] - = $ count; if $ this-> CART [$ key] ['count'] <= 0) unset ($ this-> Cart [$ key]);} Return $ this -> _ getArray ($ this-> cart);} //} }} / {{{{MODI ($ KEY, $ VALUE)

/ ** * Modify the number of items for the shopping cart to $ Key $ value * * @access public * @Param string $ key Product ID * @Param int $ value product quantity * @Return Array Return to the current shopping cart Array of goods; * / function modi ($ key, $ value) {if (! $ This -> _ iesxists ($ key)) Return $ this -> _ getArray ($ this-> cart); // does not exist this product, Return to IF directly ($ value <= 0) {// Value is too small, all delete unset ($ this-> CART [$ key]); return $ this -> _ getArray ($ this-> cart);} $ this- > CART [$ key] ['count'] = $ value; returnid $ this -> _ getArray ($ this-> cart);}

/ ** * Return the array of current shopping carts * * @access public * @Return Array Returns the array of goods in the current shopping cart; * / function getcart () {Return $ this -> _ getArray ($ this-> cart) }

//}}} // {{{_isexists ($ key)

/ ** * Determined whether there is a product ID number $ key in the current shopping cart * * @access private * @Param string $ key product id * @return bool true @Return Bool True or false; * / function _iseXists ($ key) {ix (isset ($ THIS-> CART [$ Key]) &&! Empty ($ THIS-> CART [$ Key]) && array_key_exists ($ KEY, $ this-> cart) Return true; return false;} //}} } // {{{_getarray ()

/ ** * Replace the product in the shopping cart to read * @access private * @Arty Array Return the goods in the shopping cart; * / function _GetArray ($ array) {$ mycart = array (); Foreach ($ Array As $ Item) Array_Push ($ MyCart, $ Item); Return $ mycart;}

//}}} // {{{iSempty ()

/ ** * Judging whether the current shopping cart is empty, that is, there is no product * * @access public * @return bool true or false; * / function isempty () {Return! Count ($ this-> cart);}

//}}} // {{{_STAT ()

/ ** * acquisition partial statistics * * @access private * @return bool true or false; * / function _stat () {if ($ this-> iSempty ()) Return False; Foreach ($ this-> CART AS $ Item) {$ this-> totalcount = @end ($ this-> totalprices = @prev ($ it);} return true;

//}}} // {{{TotalPrices ()

/ ** * Get the total amount of all products of the current shopping cart * * @access public * @Return float returned the amount; * / function TotalPrices () {if ($ this -> _ stat ()) Return $ this-> TotalPrices; Return }

//}}} // {{{iSempty ()

/ ** * Total number of products from the current shopping cart and * * @access public * @Return int; * / function Totalcount () {($ this -> _ stat ()) Return $ this-> Totalcount; return 0 }

} // End Class Cart

session_start (); if (! isset ($ _ session ['cart')) {$ _SESSION ['CART'] = new cart;}?>

转载请注明原文地址:https://www.9cbs.com/read-124721.html

New Post(0)