PHP using cookies to implement the shopping cart tutorial case

Source: Internet
Author: User
This article mainly introduces the way of using cookies to realize the shopping cart, can use cookies to realize the function of adding and removing goods, as well as the statistics and inspection skills, very practical value, the need for friends can refer to.
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:
The code is as follows:

<?php/** * Shopping cart cookies are stored for 1 days Note: The browser must support cookies to be able to use */class Cartapi {Private $cartarray = array ();//Store cart Two-dimensional array private $cartcount; Statistics Shopping Cart number public $expires = 86400; Cookie expiration time, if 0 is not saved to local unit in seconds/** * constructor Initialization action if $id is not empty, add directly to cart */Public Function __construct ($id = "", $nam E = "", $price 1 = "", $price 2 = "", $price 3 = "", $count = "", $image = "", $expires = 86400) {if ($id! = "" "&& Is_nu    Meric ($id)) {$this->expires = $expires;   $this->addcart ($id, $name, $price 1, $price 2, $price 3, $count, $image); }}/** * Add Item to CART * * @param int $id Item number * @param string $name product Name * @param decimal $price 1 commodity price * @para M decimal $price 2 commodity price * @param decimal $price 3 commodity price * @param int $count Item quantity * @param string $image product image * @retur N If the commodity 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 (); Read and write the data to the array if ($this->checkitem ($id)) {//Detection of the existence $this->modifycart ($id, $count, 0);   Product quantity 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 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 If the modification fails, returns false */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 generally $count to 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://Empty product 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 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 the cart for items * * @return bool If there is a commodity, return true, otherwise false */Public function Checkcart () {$tmparray = $this-&G   T;cartview ();   if (count ($tmparray [0]) < 1) {return false;  } return true; }/** * Product Statistics * * @return Array returns a one-dimensional array $arr [0]: Total price of product 1 $arr [1: Total Price of product 2 $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 array//0 is the total price of the product 1//1 is the total price of product 2//2 is the total price of 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] * $tmpar     ray[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 Product quantity * * @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 the constructor method, this method must use */Public Function Save () {$tmparray = $this->cartarray;   $tmpserialize = serialize ($tmparray);  Setcookie ("Cartapi", $tmpserialize, Time () + $this->expires);   }/** * Check whether the shopping cart item exists * * @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; }}?>

As a PHP development programmer, mastering a shopping cart skills is also a good skill, combined with the previous chapter of the content of the tutorial, I hope you have a deeper understanding of how to achieve the function of the shopping cart.

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.