PHP Single example mode to realize shopping cart function-php Instance tutorial complete this small function must write the need to develop good habits PHP shopping cart development requirements function is as follows 1: Shopping cart in Session 2: Single case mode to develop function: Add or delete to increase the reduction of a commodity
PHP Single example mode realization shopping cart function-php Example Tutorial complete this small function must write the need to develop good habits
PHP Shopping Cart Development requirements function as follows: php100.com
1: Shopping Cart in session
2: Single case mode to develop
Function:
Check and delete and change
Add a Product
Reduce one commodity (change, quantity)
By deleting
Get rid of a product
Empty shopping Cart
Check:
Back to all Items list
A total of several commodities
A total of several commodities
How much does the merchandise in the shopping cart cost?
When you understand the PHP shopping cart principle and then write code is very easy to write the following shopping cart class
The PHP code is as follows:
Class cart{
static protected $ins; Instance variables
protected $item = Array (); Put in a commodity container
Suppress external calls
Final protected function __construct () {
}
Prohibit cloning
Final protected function __clone () {
}
Internal instantiation of a class
Static protected function Getins () {
if (!) ( Self:: $ins instanceof Self)) {
Self:: $ins = new self ();
}
Return self:: $ins;
}
In order to enable the product to save across the page, the object into the session
Public Function Getcat () {
if (!) ( $_session[' cat '])! ($_session[' cat '] instanceof self)) {
$_session[' cat '] = Self::getins ();
}
Return $_session[' cat '];
}
Row test, whether in the $item existence.
Public Function Initem ($goods _id) {
if ($this->gettype () = = 0) {
return false;
}
if (!) ( Array_key_exists ($goods _id, $this->item))) {
return false;
}else{
return $this->item[$goods _id][' num ']; Returns the number of items
}
}
Add a Product
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 (); A product is an array
$this->item[$goods _id][' num ' = $num; The purchase quantity of this one commodity
$this->item[$goods _id][' name ' = $name; Product Name
$this->item[$goods _id][' price ' = $price; Commodity price
}
Reduce a commodity
Public Function Reduceitem ($goods _id, $num) {
if ($this->initem ($goods _id) = = False) {
Return
}
if ($num > $this->getunm ($goods _id)) {
unset ($this->item[$goods _id]);
}else{
$this->item[$goods _id][' num ']-= $num;
}
}
Get rid of a product
Public Function Delitem ($goods _id) {
if ($this->initem ($goods _id)) {
unset ($this->item[$goods _id]);
}
}
Return to the list of purchased items
Public Function Itemlist () {
return $this->item;
}
How many kinds of goods are there?
Public Function Gettype () {
Return count ($this->item);
}
Get the total number of a product
Public Function getunm ($goods _id) {
return $this->item[$goods _id][' num '];
}
Find out how many items in a shopping cart
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
Public Function GetPrice () {
$price = 0;
if ($this->gettype () = = 0) {
return 0;
}
foreach ($this->item as $k => $v) {
$price + + $v [' num ']* $v [' num '];
}
return $price;
}
Empty shopping Cart
Public Function Emptyitem () {
$this->item = Array ();
}
}
/*
Take your own test code, too.
*/
? php
Include_once (' cart.php ');
$cart = Cart::getcat ();
$cart->additem (' 1 ', ' Espionage bandits ', ' 5 ', ' 9999 ');
Print_r ($cart);