Encap the session into a simple shopping car
php / *********************** In a simple shopping car, please refer to the corresponding article of PHP learning records. Each product requires four properties of ID, name, price, count, if you want to add new properties, modify the appropriate section.
************************* / Class Tcart {var $ sortcount; // Variety variety var $ TOTALCOST; / / Total value var worth VAR $ ID // // Id (array) VAR $ name; // Name of each type of product (array) VAR $ price; // Price of // Var $ count; / / The number of items per class (Array) VAR $ cost; // Value of each type of item (array) // ****** Constructor function tcart () {$ this-> sortcount = 0; session_start (); // Initialize a session session_register ('sID'); session_register ('sname'); session_register ('sprice'); session_register ('scount'); $ this-> update (); $ this-> Calculate ();} // **** **** Private, according to Session's Value update Class Function Update () {Global $ SID, $ SNAME, $ SPRICE, $ SCOUNT; if (! Isset ($ sID) or! Isset ($ sname) OR! ISSET ($ SPRICE)) RETURN; $ this-> id = $ sID; $ this-> name = $ sname; $ this-> price = $ sprice; $ this-> count = $ scount; $ this-> sortcount = Count ($ SID);} // ******** Private, calculate the value of each type of product according to the new data Function Calculate () {for ($ I = 0; $ i < $ this-> sortcount; $ i ) {$ this-> cost [$ i] = $ this-> price [$ I ]-> count [$ I]; $ this-> totalcost = $ this-> cost [$ I];}} // ******** ***** The following is the interface function // *** plus a product // Decision whether there is a blue existing, if there is, add count, otherwise add a new product // first is the value of session, then Call Update () and Calculate () to update member variables Function Addone ($ ID, $ NA, $ PR) {Global $ SID, $ SNAME, $ SPRICE, $ Scount; $ K = Count ($ SID); for i = 0; $ i <$ k; $ i ) {// first find this product if ($ SID [$ I] == $ ID) {$ SCOUNT [$ I] ; BREAK }} IF ($ i> =
$ K) {// No added a new product species $ sID [] = ID; $ sname [] = $ na; $ sprice [] = $ prount [] = 1;} $ this-> Update (); // Update member data $ this-> Calculate ();} // removes a commodity Function Removeone ($ ID) {Global $ SID, $ SNAME, $ SPRICE, $ Scount; $ k = Count ($ SID); For ($ I = 0; $ I <$ 1) {IF ($ SID [$ I] == $ ID) {$ SCOUNT [$ I] -; Break;}} $ this-> Update (); $ this-> Calculate ();} // Change the number of goods in Function Modifycount ($ I, $ ncount) {Global $ Scount; $ SCOUNT [$ I] = $ ncount; $ this -> Update (); $ this-> Calculate ();} // Empty a commodity Function Emptyone ($ I) {Global $ Scount; $ SCOUNT [$ I] = 0; $ this-> Update (); $ This-> Calculate ();} / ***************************************************************************************** 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.