Phpsession shopping cart-PHP source code

Source: Internet
Author: User
Tags php session
Phpsession shopping cart

Php session shopping cart

[Code] [PHP] code

 IncNum ($ id, $ num); return ;}$ item = array (); $ item ['id'] = $ id; $ item ['name'] = $ name; $ item ['price'] = $ price; $ item ['num'] = $ num; $ item ['IMG '] = $ img; $ _ SESSION ['cart'] [$ id] = $ item ;} /* modify the number of items in the shopping cart int $ id item primary key int $ num the number of items after modification, change the quantity of a product to $ num */public function modNum ($ id, $ num = 1) {if (! Isset ($ _ SESSION ['cart'] [$ id]) {return false ;} $ _ SESSION ['cart'] [$ id] ['num'] = $ num;}/* Item Quantity + 1 */public function incNum ($ id, $ num = 1) {if (isset ($ _ SESSION ['cart'] [$ id]) {$ _ SESSION ['cart'] [$ id] ['num'] + = $ num;}/* Item Quantity-1 */public function decNum ($ id, $ num = 1) {if (isset ($ _ SESSION ['cart'] [$ id]) {$ _ SESSION ['cart'] [$ id] ['num']-= $ num;} // after reduction, the number is 0, delete this item if ($ _ SESSION ['cart'] [$ id] ['num'] <1) {$ this-> delItem ($ id );}} /* delete item */public function delItem ($ id) {unset ($ _ SESSION ['cart'] [$ id]);} /* obtain a single item */public function getItem ($ id) {return $ _ SESSION ['cart'] [$ id];} /* query the types of items in the shopping cart */public function getCnt () {return count ($ _ SESSION ['cart']);} /* query the number of items in the shopping cart */public function getNum () {if ($ this-> getCnt () = 0) {// The number of items is 0, the number is also 0 return 0;} $ sum = 0; $ data = $ _ SESSION ['cart']; foreach ($ data as $ item) {$ sum + = $ item ['num'];} return $ sum;}/* total amount of items in the shopping cart */public function getPrice () {// The quantity is 0, the price is 0 if ($ this-> getCnt () = 0) {return 0;} $ price = 0.00; $ data = $ _ SESSION ['cart']; foreach ($ data as $ item) {$ price + = $ item ['num'] * $ item ['price'];} return sprintf ("% 01.2f ", $ price);}/* clear the shopping cart */public function clear () {$ _ SESSION ['cart'] = array ();}}

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.