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 ();}}