The shopping cart of the mall is implemented in the PHP Singleton mode. -PHP source code

Source: Internet
Author: User
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 );

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.