A perfect shopping cart class

Source: Internet
Author: User
Recently, shopping cart was needed for a project, which may be frequently used. Therefore, it is encapsulated into a class for future calls. You can simply change this class a little bit can be used in your own program, the specific use of see http://bigeagle.wotoo.com/article.asp? Type1. SyntaxHighlighte

Recently, shopping cart was needed for a project, which may be frequently used. Therefore, it is encapsulated into a class for future calls. You can simply change this class a little bit can be used in your own program, the specific use of see asp $ type = 1 target = _ blank> http://bigeagle.wotoo.com/article.asp? Type = 1. SortCount = 0; session_start (); // initialize a session session_register (sId); session_register (sName); session_register (sPrice); session_register (sDiscount); session_register (sGoodPrice ); session_register (sCount); session_register (sMaxCount); $ this-> Update (); $ this-> Calculate ();} // ******* private, update function Update () {global $ sId, $ sName, $ sPrice, $ sCount, $ sDiscount, $ sMaxCount, $ sGoodPrice; if (! Isset ($ sId) or! Isset ($ sName) or! Isset ($ sPrice) or! Isset ($ sDiscount) or! Isset ($ sMaxCount) or! Isset ($ sGoodPrice) or! Isset ($ sCount) return; $ this-> Id = $ sId; $ this-> Name = $ sName; $ this-> Price = $ sPrice; $ this-> Count = $ sCount; $ this-> Discount = $ sDiscount; $ this-> GoodPrice = $ sGoodPrice; $ this-> MaxCount = $ sMaxCount; // calculate the total number of items $ this-> SortCount = count ($ sId);} // ******** private, function Calculate () {for ($ I = 0; $ I <$ this-> SortCount; $ I ++) {/* calculate the value of each item. if the Discount is 0, the Discount price */$ GiftPrice = ($ this-> Discount [$ I] = 0? $ This-> GoodPrice: ceil ($ this-> Price [$ I] * $ this-> Discount [$ I])/100 ); $ this-> TotalCost + = $ GiftPrice * $ this-> Count [$ I];} // The following *************** is the interface function // ***** plus a product // you can determine whether the product is blue or not. If yes, add count. otherwise, Add a new item. // first, change the session value. Then, call update () and calculate () to update the member variable function Add ($ a_ID, $ a_Name, $ a_Price, $ a_Discount, $ a_GoodPrice, $ a_MaxCount, $ a_Count) {global $ sId, $ sName, $ sCount, $ sPrice, $ sDiscount, $ sGoodPrice, $ sMaxCount; $ k = count ($ sId); for ($ I = 0; $ I <$ k; $ I ++) {// first check whether the product has been added to the if ($ sId [$ I] ==$ a_ID) {$ sCount [$ I] + = $ a_Count; break ;}} if ($ I >=$ k) {// if no, add a new product type $ sId [] = $ a_ID; $ sName [] = $ a_Name; $ sPrice [] = $ a_Price; $ sCount [] = $ a_Count; $ sGoodPrice [] = $ a_GoodPrice; $ sDiscount [] = $ a_Discount; $ sMaxCount [] = $ a_MaxCount;} $ this-> Update (); // Update the member data of the class $ this-> Calculate ();} // Remove a commodity function Remove ($ a_ID) {global $ sId, $ sName, $ sCount, $ sPrice, $ sDiscount, $ sGoodPrice, $ sMaxCount; $ k = count ($ sId); for ($ I = 0; $ I <$ k; $ I ++) {if ($ sId [$ I] ==$ a_ID) {$ sCount [$ I] = 0; break ;}$ this-> Update (); $ this-> Calculate () ;}// function ModifyCount ($ a_ I, $ a_Count) {global $ sCount; $ sCount [$ a_ I] = $ a_Count; $ this-> Update (); $ this-> Calculate ();} /***************************** clear all commodities ******** * *******************/function RemoveAll () {Register (sId); session_unregister (sName); session_unregister (sPrice); session_unregister (sDiscount); session_unregister (sGoodPrice); summary (sCount); summary (sMaxCount ); $ this-> SortCount = 0; $ this-> TotalCost = 0;} // whether a product is already in blue. the parameter is the ID function Exists ($ a_ID) of the product) {for ($ I = 0; $ I <$ this-> SortCount; $ I ++) {if ($ this-> Id [$ I] = $ a_ID) return TRUE;} return FALSE;} // function IndexOf ($ a_ID) {for ($ I = 0; $ I <$ this-> SortCount; $ I ++) {if ($ this-> Id [$ I] ==$ id) return $ I;} return 0 ;} // obtain the information of a product. The main function // returns an associated array, function Item ($ I) {$ Result [id] = $ this-> Id [$ I]; $ Result [name] = $ this-> Name [$ I]; $ Result [price] = $ this-> Price [$ I]; $ Result [count] = $ this-> Count [$ I]; $ Result [discount] = $ this-> Discount [$ I]; $ Result [goodprice] = $ this-> GoodPrice [$ I]; $ Result [maxcount] = $ this-> MaxCount [I]; return $ Result;} // obtain the total number of product categories function CartCount () {return $ this-> SortCount ;} // function GetTotalCost () {return $ this-> TotalCost ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.