A PHP shopping car

xiaoxiao2021-03-06  104

* @package cart * @category cart * @License php license * @access public * @version $ revision: 1.10 $ * / 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 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?>

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 modi ($ key [$ I], $ VALUE [$ I]);}}

$ items = $ cart-> getcart (); // Print echo "

"; setLocale (lc_monetary, 'it_it'); Foreach ($ items as $ item) {echo "; Echo"
ID: ". $ item ['id']." "; echo " Products:" $ item ['name']; echo " unit price:" $ item ['price']; echo $ sum = $ item ['count'] * $ item ['price']; echo Total: ". Round ($ SUM, 2); Echo" ";} echo" "; echo"
"; echo" ";>
ID: Product Name: Unit Price: quantity:

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.041, SQL: 9