PHP uses cookies to implement shopping cart method _php Tips

Source: Internet
Author: User

This example describes the way PHP uses cookies to implement a shopping cart. Share to everyone for your reference. The specific analysis is as follows:

The PHP shopping cart is used on ecommerce sites, a supermarket like a shopping cart, select the goods, first put into their own shopping cart inside and so on to the counter checkout, this section of the PHP shopping cart completely follow this principle to the example, interested friends can take a look at this instance using cookies to achieve, The code is as follows:

Copy Code code as follows:
<?php
/**
* Shopping Cart class cookies save for 1 days Note: Browsers must support cookies to be able to use
*/
Class Cartapi {
Private $cartarray = Array (); Store a two-dimensional array of shopping carts
Private $cartcount; Count the number of shopping carts
Public $expires = 86400; Cookie expiration time, if 0 is not saved to the local unit is seconds
/**
* constructor Initialization If the $id is not empty, add it directly to the shopping 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 merchandise to Shopping cart
*
* @param int $id number of items
* @param string $name commodity name
* @param decimal $price 1 commodity prices
* @param decimal $price 2 commodity prices
* @param decimal $price 3 Commodity prices
* @param int $count number of items
* @param string $image merchandise picture
* @return If the product exists, 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 (); Reading and writing data to an array
if ($this->checkitem ($id)) {//detect the existence of a commodity
$this->modifycart ($id, $count, 0); Quantity of goods 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 the goods in the shopping cart
*
* @param int $id Item number
* @param int $count number of items
* @param int $flag Modify type 0: plus 1: minus 2: Modify 3: Empty
* @return returns False if the modification fails
*/
Public Function Modifycart ($id, $count, $flag = "") {
$tmpid = $id;
$this->cartarray = $this->cartview (); Reading and writing 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://Decrease in 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://Empty goods
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 whether the shopping cart has merchandise
*
* @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;
}
/**
* Commodity Statistics
*
* @return Array returns a one-dimensional array $arr [0]: The total price of the product 1 $arr [1: Product 2 The Total price $arr [2]: Product 3 of the total price $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 quantity of the product
$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 on the number of commodities
*
* @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 constructor method, this method must use the
*
*/
Public Function Save () {
$tmparray = $this->cartarray;
$tmpserialize = serialize ($tmparray);
Setcookie ("Cartapi", $tmpserialize, Time () + $this->expires);
}
/**
* Check whether the shopping cart product exists
*
* @param int $id
* @return BOOL If TRUE or 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 help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.