Shopping Cart Category
Analysis Construction Car:
1: No matter how many times you refresh the site, or how many new items have been added,
When you look at your shopping cart, you see the same results.
That is: you open a product refresh, B product Refresh, home, see the shopping cart should be the same.
Or, the whole site, shopping cart--is the overall effective!
Solution: Put the shopping cart information in the database, you can also put in the Session/cookie
2: Since it is globally valid, it implies that there are only 1 instances of a shopping cart!
Can not say on 3 pages, bought 3 items, the formation of 3 shopping cart instances, which is obviously unreasonable.
Workaround: Single-case mode
Technology selection: session+ single case
Functional Analysis:
Determine if a product exists
Add Item
Delete Item
Modify the quantity of a product
Number of items plus 1
The quantity of a product minus 1
Inquire about the product type of shopping cart
Check the number of items in your shopping cart
Check the total amount of merchandise in the shopping cart
Return to all items in the shop
Empty shopping Cart
<?phpdefined (' ACC ') | | Exit (' Acc deined '); class Carttool {private static $ins = null; Private $items = Array (); Final protected function __construct () {} final protected function __clone () {}//Get instance protected static function Getins () {if (!) ( Self:: $ins instanceof Self)} {self:: $ins = new self (); } return Self:: $ins; }//Put the cart's singleton object in the SESSION public static function Getcart () {if (!isset ($_session[' cart ')) | |! ( $_session[' cart '] instanceof self) {$_session[' cart '] = self::getins (); } return $_session[' cart '; }/* Add commodity param int $id Commodity primary key param string $name product name param float $price commodity price param int $num Number of purchases */ Public Function AddItem ($id, $name, $price, $num =1) {if ($this->hasitem ($id)) {//If the item already exists, add the quantity directly $this->incnum ($id, $num); Return } $item = Array (); $item [' name '] = $name; $item[' price '] = $price; $item [' num '] = $num; $this->items[$id] = $item; }/* Modify the number of items in the cart param int $id commodity primary key param int $num The quantity of a product modified, that is, the quantity of a product is changed directly to $num */Public function Modnum ($id, $num =1) {if (! $this->hasitem ($id)) {return false; } $this->items[$id [' num '] = $num; }/* The number of items increased by 1 */Public Function incnum ($id, $num =1) {if ($this->hasitem ($id)) {$this items[$id [' num '] + = $num; }}/* The number of items decreased by 1 */Public Function decnum ($id, $num =1) {if ($this->hasitem ($id)) {$this ->items[$id] [' num ']-= $num; }//If the number is reduced to 0, delete the item from the shopping cart if ($this->items[$id [' num '] < 1) {$this->delitem ($id); }}/* Determine if an item exists */Public Function Hasitem ($id) {return array_key_exists ($id, $this->i TEMS); }/* Delete goods */Public Function Delitem ($id) {unset ($this->items[$id]); }/* Query the type of goods in the shopping cart */Public Function getcnt () {return count ($this->items); }/* Query the number of items in the cart */Public Function Getnum () {if ($this->getcnt () = = 0) {return 0; } $sum = 0; foreach ($this->items as $item) {$sum + = $item [' num ']; } return $sum; }/* Query the total amount of items in the cart */Public Function GetPrice () {if ($this->getcnt () = = 0) {return 0 ; } $price = 0.0; foreach ($this->items as $item) {$price + = $item [' num '] * $item [' price ']; } return $price; }/* Returns all items in the shopping cart */Public Function all () {return $this->items; }/* Empty shopping cart */Public Function Clear () {$this->items = array (); }}/*session_start ();//Print_r (Carttool::getcart ()); $cart = Carttool::getcart (); if (!isset ($_get[' test ')) {$_get[') Test '] = ';}if ($_get[' test '] = = ' Addwangba ') {$cart->additem (1, ' King VIII ', 23.4, 1); Echo ' Add Wangba ok ';} else if ($_get[' test '] = = ' Addfz ') {$cart->additem (2, ' Ark ', 2347.56, 1); Echo ' Add Fangzhou ok ';} else if ($_get[' test ' = = ' clear ') {$cart->clear ();} else if ($_get[' test '] = = ' show ') {Print_r ($cart->all ()); echo ' <br/> '; Echo ' Total ', $cart->getcnt (), ' species ', $cart->getnum (), ' commodity <br/> '; Echo ' Total ', $cart->getprice (), ' Yuan ';} else {print_r ($cart);} */?>
Shopping Cart Category