phpclass cart {var $ sortcount; // Product species Var $ TOTALCOST; // Products total amount / * All items, such as $ mycart [3] [$ name]: Product number 3 Name * $ MyCart [3] [$ price]: Product number 3 unit price * $ mycart [3] [$ count]: Product number 3 * $ mycart [3] [$ 20]: Total amount of item number 3 * * / Var $ mycart; var $ ID; // // The ID (array) of each type of item (array) var $ name; // Name of each type of item (array) var $ price; // Price of Each type of item (array) Var $ count ; // Number of pieces of each type of item (array) Var $ cost; // Value of each type of item (array)
// ****** Constructor function cart () {$ this-> sortcount = 0; $ this-> totalcost = 0; $ this-> mycart = array (); session_start (); // Initialize a session IF (session_is_register ("mycart") == false) session_register ('mycart'); $ this-> Update (); // $ this-> Calculate ();} // ******** Private, According to the value of the session, the corresponding data function update () {session_start (); // Initialize a session $ mycart = $ _SESSION ["Mycart"]; if (false == $ mycart) {$ this-> sortcount = 0 $ This-> totalcost = 0; $ this-> mycart = array (); return false;} // get the total number of items $ this-> sortcount = count ($ mycart); if ($ this-> sortcount> 0) {// Start calculation of items $ TOTALCOST = 0; Foreach ($ MyCart AS $ Key => $ VAL) {// First Self 20 into Foreach ($ VAL AS $ PRONAME => $ Providence) {IF ($ PRONAME! = "Name") {$ VAL [$ proname] = Round (EREGI_REPLACE (",", ",", $ propal), 2); $ MyCart [$ Key] = $ VAL [$ PRONAME];}} // Calculate $ MyCart [$ Key] ["Cost"] = Round ($ VAL ["COUNT"] * $ VAL ["price"], 2); // Get all items $ TOTALCOST = $ MyCart [$ key] ["cost"];} $ this-> totalcost = $ totalcost; $ this-> mycart = $ mycart; $ _SESSION ["mycart"] = $ mycart;
}}} / ** * Format numbers for currency data * * ** / function formatnum ($ data) {Foreach ($ DATA AS $ Key => $ VAL) {Foreach ($ VAL AS $ SNAME => $ sVALUE) { IF ($ SNAME! = "name") {$ data [$ key] [$ SNAME] = Number_Format ($ sValue, 2);}}} Return $ data;} // *********** **** The following is the interface function // *** Add a product // Judgment whether there is already blue, if there is, plus count, otherwise add a new product // first is the value of the session, then call Update () and Calculate () to update member variables Function Addone ($ ID, $ NA, $ P) {session_start (); // Initialize a session $ mycart = $ _SESSION ["mycart"]; // Set the shopping cart Number $ MyCart [$ ID] ["name"] = $ na; $ mycart [$ ID] ["price"] = $ PR; $ mycart [$ ID] ["count"]; $ _SESSION [" Mycart "] = $ mycart; // Update the member data of the class $ this-> Update ();
} / ** * Add a set of items to your cart, if not, add, if you already exist, update to Data * @Param $ data - The product to add, the format is: * $ data [0] [ID ], $ Data [0] [name], * $ data [0] [price], $ data [0] [count] * @return boolean * / function adddata ($ data) {ified (count ($ data> 0) )) {Session_start (); // Initialize a session $ mycart = $ _SESSION ["MyCart"]; Foreach ($ data as $ val) {extract ($ val); // Set the number of shopping carts [$ mYcart [$ ID] ["name"] = $ name; $ mycart [$ ID] ["price"] = $ price; $ mycart [$ ID] ["count"] = $ count;} $ _SESSION ["Mycart"] = $ mycart; // Update a member data $ this-> Update ();}} / ** Change a single price ***** / function Updateprice ($ ID, $ price) {if ($ Price) <= 0) Return False; session_start (); // Initialize a session $ mycart = $ _SESSION ["MyCart"]; if ($ MyCart [$ ID] == True) {$ MyCart [$ ID] ["price" ] = $ price;
$ _SESSION ["Mycart"] = $ mycart; $ this-> Update ();}} // minus one item 1 Function Removeone ($ ID) {$ count = $ this-> mycart [$ ID] ["count"]; if ($ count> 0) {$ this-> modifycount ($ ID, - $ count);}} // change the number of items, if you pass a single price, change the unit price Function ModifyCount together ($ ID, $ ncount, $ price = 0) {if ($ ncount <= 0) Return false; session_start (); // Initialize a session $ mycart = $ _SESSION ["mycart"]; if ($ mycart [$ ID] == True) {$ mycart [$ ID] ["count"] = $ ncount; // If there is a single price, change the unit price if ($ price> 0) $ mycart [$ ID] ["Price "] = $ price;
$ _SESSION ["MyCart"] = $ mycart; $ this-> Update ();}} // Clear a commodity Function Emptyone ($ i) {session_start (); // Initialize a session $ mycart = $ _SESSION [" Mycart "]; unset ($ mycart [$ i]); if ($ mycart) == 0) {$ this-> EMPTYALL ();} else {$ _SESSION [" mycart "] = $ mycart; $ this -> Update ();}} / ************************** Clear all items Because PHP in Win does not support session_destroy () The function, so this emptying function is not perfect, but the number of each item is 0. If it is under Linux, you can use session_destroy () directly. ************************************************ / FUNCTION EMPTYALL () {session_start (); // Initialize a session $ mycart = $ _SESSION [ "mycart"]; unset ($ mycart); $ _SESSION ["mycart"] = $ mycart; $ this-> update ();} / ** * Return to all shopping carts * ** / function getData () {IF ($ this-> sortcount> 0) {Return $ this-> mycart;} else {return array ();}} // Take information on one item, the main work function // returns an associated array, under Corresponds to ID, Name, Price, Count, Cost Function Getone ($ DATA = $ this-> mycart [$ I]; if (false == $ data) Return Array (); $ data ["id "] = $ I; returnid $ data;