The shopping cart of the mall is implemented in the PHP Singleton mode.
1.
[Code][PHP] code
Gettype () = 0) {return false;} if (! (Array_key_exists ($ goods_id, $ this-> item) {return false;} else {return $ this-> item [$ goods_id] ['num']; // return the number of items} // add a public function Additem ($ goods_id, $ name, $ num, $ price) {if ($ this-> Initem ($ goods_id )! = False) {$ this-> item [$ goods_id] ['num'] + = $ num; return;} $ this-> item [$ goods_id] = array (); // an array of items $ this-> item [$ goods_id] ['num'] = $ num; // quantity purchased for this item $ this-> item [$ goods_id] ['name'] = $ name; // product name $ this-> item [$ goods_id] ['price'] = $ price; // product unit price} // reduce the public function performanceitem ($ goods_id, $ num) {if ($ this-> Initem ($ goods_id) = false) {return;} if ($ num> $ this-> Getunm ($ goods_id )) {unset ($ t His-> item [$ goods_id]);} else {$ this-> item [$ goods_id] ['num']-= $ num ;}} // remove a commodity public function Delitem ($ goods_id) {if ($ this-> Initem ($ goods_id )) {unset ($ this-> item [$ goods_id]) ;}// return to the purchased item list public function Itemlist () {return $ this-> item ;} // total number of items public function Gettype () {return count ($ this-> item);} // obtain the total number of items public function Getunm ($ goods_id) {return $ this-> item [$ goods_id] ['num'];} // Query how many items in the shopping cart are public function Getnumber () {$ num = 0; if ($ this-> Gettype () = 0) {return 0 ;} foreach ($ this-> item as $ k => $ v) {$ num + = $ v ['num'];} return $ num ;} // calculate the total price of public function Getprice () {$ price = 0; if ($ this-> Gettype () = 0) {return 0 ;} foreach ($ this-> item as $ k => $ v) {$ price + = $ v ['num'] * $ v ['num'];} return $ price;} // clear the shopping cart public function Emptyitem () {$ this-> item = array () ;}/ * test the code by yourself. Also take out */? Phpinclude_once ('cart. php '); $ cart = Cart: Getcat (); $ cart-> Additem ('1', 'bandit', '5', '123 '); print_r ($ cart );