<? Php /** * The storage cycle of shopping cart cookies is one day. Note: The browser must support cookies for use. * Technical exchange group: 100352308 */ Class cartapi { Private $ cartarray = array (); // Two-dimensional array for storing the shopping cart Private $ cartcount; // count the number of shopping carts Public $ expires = 86400; // cookie expiration time. If it is 0, it is not saved to the local device, in seconds. /** * If $ id is not empty, the constructor is directly added to the shopping cart. * */ Public function _ construct ($ id = "", $ name = "", $ price1 = "", $ price2 = "", $ price3 = "", $ count = "", $ image = "", $ expires = 86400 ){ If ($ id! = "" & Is_numeric ($ id )){ $ This-> expires = $ expires; $ This-> addcart ($ id, $ name, $ price1, $ price2, $ price3, $ count, $ image ); } } /** * Add items to the shopping cart * * @ Param int $ id of the product * @ Param string $ name Product name * @ Param decimal $ price1 product price * @ Param decimal $ price2 product price * @ Param decimal $ price3 product price * @ Param int $ count number of items * @ Param string $ image product image * @ Return if the product exists, add 1 to the original quantity and return false. */ Public function addcart ($ id, $ name, $ price1, $ price2, $ price3, $ count, $ image ){ $ This-> cartarray = $ this-> cartview (); // reads data and writes it to an array. If ($ this-> checkitem ($ id) {// check whether the product exists $ This-> modifycart ($ id, $ count, 0); // add $ count to the number of items Return false; } $ This-> cartarray [0] [$ id] = $ id; $ This-> cartarray [1] [$ id] = $ name; $ This-> cartarray [2] [$ id] = $ price1; $ This-> cartarray [3] [$ id] = $ price2; $ This-> cartarray [4] [$ id] = $ price3; $ This-> cartarray [5] [$ id] = $ count; $ This-> cartarray [6] [$ id] = $ image; $ This-> save (); } /** * Modify the items in the shopping cart * * @ Param int $ id product number * @ Param int $ count number of items * @ Param int $ flag modify type 0: Add 1: subtract 2: Modify 3: clear * @ Return if the modification fails, false is returned. */ Public function modifycart ($ id, $ count, $ flag = ""){ $ Tmpid = $ id; $ This-> cartarray = $ this-> cartview (); // reads data and writes it to an array. $ Tmparray = & $ this-> cartarray; // reference If (! Is_array ($ tmparray [0]) return false; If ($ id <1 ){ Return false; } Foreach ($ tmparray [0] as $ item ){ If ($ item ===$ tmpid ){ Switch ($ flag ){ Case 0: // add quantity. Generally, $ count is 1. $ Tmparray [5] [$ id] + = $ count; Break; Case 1: // reduce the number $ Tmparray [5] [$ id]-= $ count; Break; Case 2: // modify the quantity If ($ count = 0 ){ Unset ($ tmparray [0] [$ id]); Unset ($ tmparray [1] [$ id]); Unset ($ tmparray [2] [$ id]); Unset ($ tmparray [3] [$ id]); Unset ($ tmparray [4] [$ id]); Unset ($ tmparray [5] [$ id]); Unset ($ tmparray [6] [$ id]); Break; } Else { $ Tmparray [5] [$ id] = $ count; Break; } Case 3: // clear items Unset ($ tmparray [0] [$ id]); Unset ($ tmparray [1] [$ id]); Unset ($ tmparray [2] [$ id]); Unset ($ tmparray [3] [$ id]); Unset ($ tmparray [4] [$ id]); Unset ($ tmparray [5] [$ id]); Unset ($ tmparray [6] [$ id]); Break; Default: Break; } } } $ This-> save (); } /** * Clear the shopping cart * */ Public function removeall (){ $ This-> cartarray = array (); $ This-> save (); } /** * View Shopping Cart Information * * @ Return array returns a two-dimensional array. */ Public function cartview (){ $ Cookie = strips tutorial lashes ($ _ cookie ['cartapi ']); If (! $ Cookie) return false; $ Tmpunserialize = unserialize ($ cookie ); Return $ tmpunserialize; } /** * Check whether there are items in the shopping cart. * * @ Return bool returns true if there is a commodity; otherwise, false */ Public function checkcart (){ $ Tmparray = $ this-> cartview (); If (count ($ tmparray [0]) <1 ){ Return false; } Return true; } /** * Product Statistics * * @ Return array returns a one-dimensional array $ arr [0]: total price of product 1 $ arr [1: total price of product 2 $ arr [2]: total price of product 3 $ arr [3]: total number of products */ Public function countprice (){ $ Tmparray = $ this-> cartarray = $ this-> cartview (); $ Outarray = array (); // one-dimensional array // 0 is the total price of product 1 // 1 is the total price of product 2 // 2 is the total price of product 3 // 3 is the total number of products $ I = 0; If (is_array ($ tmparray [0]) { Foreach ($ tmparray [0] as $ key => $ val ){ $ Outarray [0] + = $ tmparray [2] [$ key] * $ tmparray [5] [$ key]; $ Outarray [1] + = $ tmparray [3] [$ key] * $ tmparray [5] [$ key]; $ Outarray [2] + = $ tmparray [4] [$ key] * $ tmparray [5] [$ key]; $ Outarray [3] + = $ tmparray [5] [$ key]; $ I ++; } } Return $ outarray; } /** * Counting commodity quantity * * @ Return int */ Public function cartcount (){ $ Tmparray = $ this-> cartview (); $ Tmpcount = count ($ tmparray [0]); $ This-> cartcount = $ tmpcount; Return $ tmpcount; } /** * If you do not use the constructor to save a product, this method must be used. * */ Public function save (){ $ Tmparray = $ this-> cartarray; $ Tmpserialize = serialize ($ tmparray ); Setcookie ("cartapi", $ tmpserialize, time () + $ this-> expires ); } /** * Check whether the shopping cart item exists * * @ Param int $ id * @ Return bool if true exists, otherwise false */ Private function checkitem ($ id ){ $ Tmparray = $ this-> cartarray; If (! Is_array ($ tmparray [0]) return; Foreach ($ tmparray [0] as $ item ){ If ($ item ===$ id) return true; } Return false; } } ?> |