PHP code to achieve a shopping cart storage cycle of 1 days

Source: Internet
Author: User
Shopping cart cookies are stored for 1 days. Note: Browsers must support cookies to be able to use them. This article mainly and everyone introduced in detail the PHP implementation of the storage cycle for 1 days shopping cart category, with a certain reference value, interested in small partners can refer to, hope to help everyone.

Example code:


<?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 two-dimensional array of cart 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_n      Umeric ($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-&gt ; 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 General $co          UNT is 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 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 = stripslashes ($_cookie[' C    Artapi ']);    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-& Gt    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] * $        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 goods * * @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; }}?>

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.