/ ** * cart * * Shopping car * * @Author doodoo
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 Array of Inner Commodities * / Function Add ($ Item) {if (! Is_ARRAY ($ Item) | IS_NULL ($ Item)) Return $ this-> Cart; if (! Is_numeric (end ($ item)) || (! is_numeric)))) {echo "price and quantity must be a number"; Return $ this-> Cart;} reset ($ item); // This sentence is must, because the above judgment has already Move the array of indicators $ key = current ($ item); if ($ key == ") Return $ this-> cart; if ($ this -> _ meansxists ($ key)) {// Is there? $ This-> cart [$ key] ['count'] = end ($ it); return $ 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-> 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-> 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 The array of goods; * / function modi ($ key, $ value) {if (! $ This -> _ iesxists ($ key)) Return $ this-> Cart (); // does not exist, return to IF directly ($ Value <= 0) {// Value is too small, all delete unset ($ this-> CART [$ key]); return $ this-> cart;} $ this-> cart [$ key] ['count'] = $ Value; Return $ this-> Cart;
/ ** * Returns the array of current shopping carts * * @access public * @Return Array Returns the array of goods in the current shopping cart; * / function getcart () {Return $ 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;} //}} } // {{{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?>
PHP // Call instance Require_once 'cart.class.php'; session_start (); if (! isset ($ _ session ['cart')) {$ _SESSION ['cart'] = new cart;} $ cart = & $ _SESSION ['Cart'];
IF ($ _SERVER ['Request_Method'] == "POST") && ($ _ post ") == 'add') {$ P = $ _POST ['P']; $ items = $ CART- > add ($ p);} if ($ _Get ['Action'] == 'Remove') && ($ _ get [ky ']! = "") {$ items = $ cart-> remove ($ _Get ['Key']);} IF ($ _SERVER ['Request_Method'] == "POST") && ($ _ post ['Action'] == 'MODI')) {$ key = $ _POST ['Key ']; $ Value = $ _POST [' Value ']; for ($ I = 0; $ i
$ items = $ cart-> getcart (); // Print echo "