PHP Shopping cart is in the e-commerce site will be used, like a supermarket shopping cart, choose a good product, first put in their own shopping cart and so on to the counter settlement, this section of the PHP shopping cart in accordance with the principle of the example, the following we take a look at it, the use of cookies to achieve.
PHP Tutorial Shopping cart is used in e-commerce site, a supermarket like shopping cart, choose a good product, first put in their own shopping cart and so on to the counter settlement, this section of the PHP shopping cart in accordance with the principle of the example, the following we take a look at it, the use of cookies to achieve.
/**
* Shopping cart cookies are stored for 1 days. Note: Browsers must support cookies to be able to use
* Technology Exchange Group: 100352308
*/
Class Cartapi {
Private $cartarray = Array (); Two-dimensional array for storing shopping carts
Private $cartcount; Statistics Shopping Cart Quantity
Public $expires = 86400; Cookie expiration time, if 0 is not saved to local unit for seconds
/**
* Constructor Initialization operation if $id is not empty, add directly to Cart
*
*/
Public function __construct ($id = "", $name = "", $price 1 = "", $price 2 = "", $price 3 = "", $count = "", $image = "", $expires = 86400) {
if ($id! = "" "&& Is_numeric ($id)) {
$this->expires = $expires;
$this->addcart ($id, $name, $price 1, $price 2, $price 3, $count, $image);
}
}
/**
* Add items to Cart
*
* Number of @param int $id item
* @param string $name product Name
* @param decimal $price 1 product price
* @param decimal $price 2 product price
* @param decimal $price 3 Product price
* @param int $count number of items
* @param string $image product picture
* @return If the product is present, add 1 to the original quantity and return False
*/
Public Function Addcart ($id, $name, $price 1, $price 2, $price 3, $count, $image) {
$this->cartarray = $this->cartview (); Read and write data to an array
if ($this->checkitem ($id)) {//detects if the product exists
$this->modifycart ($id, $count, 0); Number of items plus $count
return false;
}
$this->cartarray[0][$id] = $id;
$this->cartarray[1][$id] = $name;
$this->cartarray[2][$id] = $price 1;
$this->cartarray[3][$id] = $price 2;
$this->cartarray[4][$id] = $price 3;
$this->cartarray[5][$id] = $count;
$this->cartarray[6][$id] = $image;
$this->save ();
}
/**
* Modify items in your shopping cart
*
* @param int $id Product number
* @param int $count number of items
* @param int $flag Modify type 0: plus 1: minus 2: Modify 3: Clear
* @return returns False if the modification fails
*/
Public Function Modifycart ($id, $count, $flag = "") {
$tmpid = $id;
$this->cartarray = $this->cartview (); Read and write data to an array
$tmparray = & $this->cartarray; Reference
if (!is_array ($tmparray [0])) return false;
if ($id < 1) {
return false;
}
foreach ($tmparray [0] as $item) {
if ($item = = = $tmpid) {
Switch ($flag) {
Case 0://Add Quantity general $count to 1
$tmparray [5][$id] + = $count;
Break
Case 1://Reduction in quantity
$tmparray [5][$id]-= $count;
Break
Case 2://Modified Quantity
if ($count = = 0) {
Unset ($tmparray [0][$id]);
Unset ($tmparray [1][$id]);
Unset ($tmparray [2][$id]);
Unset ($tmparray [3][$id]);
Unset ($tmparray [4][$id]);
Unset ($tmparray [5][$id]);
Unset ($tmparray [6][$id]);
Break
} else {
$tmparray [5][$id] = $count;
Break
}
Case 3://Empty items
Unset ($tmparray [0][$id]);
Unset ($tmparray [1][$id]);
Unset ($tmparray [2][$id]);
Unset ($tmparray [3][$id]);
Unset ($tmparray [4][$id]);
Unset ($tmparray [5][$id]);
Unset ($tmparray [6][$id]);
Break
Default
Break
}
}
}
$this->save ();
}
/**
* Empty Shopping Cart
*
*/
Public Function RemoveAll () {
$this->cartarray = Array ();
$this->save ();
}
/**
* View Shopping cart Information
*
* @return Array returns a two-dimensional array
*/
Public Function Cartview () {
$cookie = Strips Tutorial Lashes ($_cookie[' cartapi ');
if (! $cookie) return false;
$tmpunserialize = Unserialize ($cookie);
return $tmpunserialize;
}
/**
* Check if there are any items in the shopping cart
*
* @return BOOL If there is a commodity, return true, otherwise false
*/
Public Function Checkcart () {
$tmparray = $this->cartview ();
if (count ($tmparray [0]) < 1) {
return false;
}
return true;
}
/**
* Commodity Statistics
*
* @return Array returns a one-dimensional array $arr [0]: Total price of product 1 $arr [1: Product 2 Total Price $arr [2]: Total price of product 3 $arr [3]: Total number of products
*/
Public Function Countprice () {
$tmparray = $this->cartarray = $this->cartview ();
$outarray = Array (); One-dimensional arrays
0 is the total price of the product 1
1 is the total price of the product 2
2 is the total price of the product 3
3 is the total number of products
$i = 0;
if (Is_array ($tmparray [0])) {
foreach ($tmparray [0] as $key = + $val) {
$outarray [0] + = $tmparray [2][$key] * $tmparray [5][$key];
$outarray [1] + = $tmparray [3][$key] * $tmparray [5][$key];
$outarray [2] + = $tmparray [4][$key] * $tmparray [5][$key];
$outarray [3] + = $tmparray [5][$key];
$i + +;
}
}
return $outarray;
}
/**
* Statistics of commodities
*
* @return int
*/
Public Function Cartcount () {
$tmparray = $this->cartview ();
$tmpcount = count ($tmparray [0]);
$this->cartcount = $tmpcount;
return $tmpcount;
}
/**
* Save a product if you do not use a construction method, this method must use the
*
*/
Public Function Save () {
$tmparray = $this->cartarray;
$tmpserialize = serialize ($tmparray);
Setcookie ("Cartapi", $tmpserialize, Time () + $this->expires);
}
/**
* Check whether shopping cart items exist
*
* @param int $id
* @return BOOL If present true otherwise false
*/
Private Function Checkitem ($id) {
$tmparray = $this->cartarray;
if (!is_array ($tmparray [0])) return;
foreach ($tmparray [0] as $item) {
if ($item = = = $id) return true;
}
return false;
}
}
?>
http://www.bkjia.com/PHPjc/444839.html www.bkjia.com true http://www.bkjia.com/PHPjc/444839.html techarticle PHP Shopping Cart is in the e-commerce site will be used, like a supermarket shopping cart, choose a good product, first put in their own shopping cart and so on to the counter settlement, this section of PHP shopping ...