PHP using cookies to implement the shopping cart method, the cookie shopping cart _php tutorial

Source: Internet
Author: User
Tags php programming

PHP uses cookies to implement the shopping cart method, the cookie shopping cart


This article explains how PHP uses cookies to implement a shopping cart. Share to everyone for your reference. The specific analysis is as follows:

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, interested friends can come to see, the example of the use of cookies to achieve, The code is as follows:
Copy CodeThe code is as follows: <?php
/**
* Shopping cart cookies are stored for 1 days. Note: Browsers must support cookies to be able to use
*/
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;
}
}
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/925132.html www.bkjia.com true http://www.bkjia.com/PHPjc/925132.html techarticle PHP uses cookies to implement a shopping cart, a cookie Shopping cart This example describes how PHP uses cookies to implement a shopping cart. Share to everyone for your reference. The specific analysis is as follows: P ...

  • Related Article

    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.