php uses cookies to implement shopping cart, cookies shopping cart
The example in this article describes how PHP uses cookies to implement a shopping cart. Share it with everyone for your reference. The specific analysis is as follows:
The php shopping cart is used on e-commerce websites. It is like a supermarket shopping cart. After selecting the products, you first put them in your shopping cart and then wait for them to settle at the counter. This php shopping cart The car is fully implemented according to this principle. Interested friends can take a look. This example is implemented using cookies. The code is as follows:
Copy code The code is as follows:
/**
*Shopping cart cookies are saved with a storage period of 1 day. Note: The browser must support cookies to use
*/
class cartapi {
private $cartarray = array(); // Two-dimensional array to store shopping cart
private $cartcount; // Count the number of shopping carts
public $expires = 86400; // Cookie expiration time, if it is 0, it will not be saved locally. The unit is seconds
/**
* Constructor initialization operation If $id is not empty, add it directly to the shopping cart
*
*/
public function __construct($id = "",$name = "",$price1 = "",$price2 = "",$price3 = "",$count = "",$image = "",$expires = 86400 ) {
if ($id != "" && is_numeric($id)) {
$this->expires = $expires;
$this->addcart($id,$name,$price1,$price2,$price3,$count,$image);
}
}
/**
* Add items to cart
*
* @param int $id item number
* @param string $name product name
* @param decimal $price1 Product price
* @param decimal $price2 Product price
* @param decimal $price3 Product price
* @param int $count item quantity
* @param string $image product image
* @return If the product exists, add 1 to the original quantity and return false
*/
public function addcart($id,$name,$price1,$price2,$price3,$count,$image) {
$this->cartarray = $this->cartview(); // Read and write data into the array
if ($this->checkitem($id)) { // Check whether the item exists
$this->modifycart($id,$count,0); // Add $count to the quantity of goods
Return false;
}
$this->cartarray[0][$id] = $id;
$this->cartarray[1][$id] = $name;
$this->cartarray[2][$id] = $price1;
$this->cartarray[3][$id] = $price2;
$this->cartarray[4][$id] = $price3;
$this->cartarray[5][$id] = $count;
$this->cartarray[6][$id] = $image;
$this->save();
}
/**
* Modify items in shopping cart
*
* @param int $id product number
* @param int $count item quantity
* @param int $flag Modification type 0: Add 1: Subtract 2: Modify 3: Clear
* @return If the modification fails, return false
*/
public function modifycart($id, $count, $flag = "") {
$tmpid = $id;
$this->cartarray = $this->cartview(); // Read and write data into the 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. Generally $count is 1
$tmparray[5][$id] += $count;
break;
Case 1: // Reduce quantity
$tmparray[5][$id] -= $count;
break;
Case 2: // Modify 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: // Clear 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();
}
/**
* Clear 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 items in the shopping cart
*
* @return bool If there is a product, return true, otherwise false
*/
public function checkcart() {
$tmparray = $this->cartview();
if (count($tmparray[0]) < 1) {
Return false;
}
Return true;
}
/**
* Product Statistics
*
* @return array Returns a one-dimensional array $arr[0]: the total price of product 1 $arr[1: the total price of product 2 $arr[2]: the total price of product 3 $arr[3]: the total price of product 3 Quantity
*/
public function countprice() {
$tmparray = $this->cartarray = $this->cartview();
$outarray = array(); //One-dimensional array
// 0 is the total price of product 1
// 1 is the total price of product 2
// 2 is the total price of product 3
// 3 is the total quantity 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;
}
/**
* Count the number of products
*
* @return int
*/
public function cartcount() {
$tmparray = $this->cartview();
$tmpcount = count($tmparray[0]);
$this->cartcount = $tmpcount;
return $tmpcount;
}
/**
* Save the product If you do not use the construction method, this method must use
*
*/
public function save() {
$tmparray = $this->cartarray;
$tmpserialize = serialize($tmparray);
setcookie("cartapi",$tmpserialize,time()+$this->expires);
}
/**
* Check if the items in the shopping cart exist
*
* @param int $id
* @return bool if 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;
}
}
?>
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/925132.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/925132.htmlTechArticlephp uses cookies to implement a shopping cart, cookies shopping cart This article describes how php uses cookies to implement a shopping cart . Share it with everyone for your reference. The specific analysis is as follows: p...