php代碼實現購物車儲存周期為1天

來源:互聯網
上載者:User
購物車類 Cookies 儲存,儲存周期為1天 注意:瀏覽器必須支援Cookie才能夠使用。本文主要和大家詳細介紹了php實現儲存周期為1天的購物車類,具有一定的參考價值,感興趣的小夥伴們可以參考一下,希望能協助到大家。

範例程式碼:


<?php/** * 購物車類 Cookies 儲存,儲存周期為1天 注意:瀏覽器必須支援Cookie才能夠使用 */class CartAPI {  private $CartArray = array(); // 存放購物車的二維數組  private $CartCount; // 統計購物車數量  public $Expires = 86400; // Cookies到期時間,如果為0則不儲存到本地 單位為秒  /**   * 建構函式 初始化操作 如果$Id不為空白,則直接添加到購物車   *   */  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);    }  }  /**   * 添加商品到購物車   *   * @param int $Id 商品的編號   * @param string $Name 商品名稱   * @param decimal $Price1 商品價格   * @param decimal $Price2 商品價格   * @param decimal $Price3 商品價格   * @param int $Count 商品數量   * @param string $Image 商品圖片   * @return 如果商品存在,則在原來的數量上加1,並返回false   */  public function addCart($Id,$Name,$Price1,$Price2,$Price3,$Count,$Image) {    $this->CartArray = $this->CartView(); // 把資料讀取並寫入數組    if ($this->checkItem($Id)) { // 檢測商品是否存在      $this->ModifyCart($Id,$Count,0); // 商品數量加$Count      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();  }  /**   * 修改購物車裡的商品   *   * @param int $Id 商品編號   * @param int $Count 商品數量   * @param int $Flag 修改類型 0:加 1:減 2:修改 3:清空   * @return 如果修改失敗,則返回false   */  public function ModifyCart($Id, $Count, $Flag = "") {    $tmpId = $Id;    $this->CartArray = $this->CartView(); // 把資料讀取並寫入數組    $tmpArray = &$this->CartArray; // 引用    if (!is_array($tmpArray[0])) return false;    if ($Id < 1) {      return false;    }    foreach ($tmpArray[0] as $item) {      if ($item === $tmpId) {        switch ($Flag) {          case 0: // 添加數量 一般$Count為1            $tmpArray[5][$Id] += $Count;            break;          case 1: // 減少數量            $tmpArray[5][$Id] -= $Count;            break;          case 2: // 修改數量            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: // 清空商品            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();  }  /**   * 清空購物車   *   */  public function RemoveAll() {    $this->CartArray = array();    $this->save();  }  /**   * 查看購物車資訊   *   * @return array 返回一個二維數組   */  public function CartView() {    $cookie = stripslashes($_COOKIE['CartAPI']);    if (!$cookie) return false;    $tmpUnSerialize = unserialize($cookie);    return $tmpUnSerialize;  }  /**   * 檢查購物車是否有商品   *   * @return bool 如果有商品,返回true,否則false   */  public function checkCart() {    $tmpArray = $this->CartView();    if (count($tmpArray[0]) < 1) {            return false;    }    return true;  }  /**   * 商品統計   *   * @return array 返回一個一維數組 $arr[0]:產品1的總價格 $arr[1:產品2得總價格 $arr[2]:產品3的總價格 $arr[3]:產品的總數量   */  public function CountPrice() {    $tmpArray = $this->CartArray = $this->CartView();    $outArray = array(); //一維數組    // 0 是產品1的總價格    // 1 是產品2的總價格    // 2 是產品3的總價格    // 3 是產品的總數量    $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;  }  /**   * 統計商品數量   *   * @return int   */  public function CartCount() {    $tmpArray = $this->CartView();    $tmpCount = count($tmpArray[0]);    $this->CartCount = $tmpCount;    return $tmpCount;  }  /**   * 儲存商品 如果不使用構造方法,此方法必須使用   *   */  public function save() {    $tmpArray = $this->CartArray;    $tmpSerialize = serialize($tmpArray);    setcookie("CartAPI",$tmpSerialize,time()+$this->Expires);  }  /**   * 檢查購物車商品是否存在   *   * @param int $Id   * @return bool 如果存在 true 否則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;  }}?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.