PHP利用cookies實現購物車教程案例

來源:互聯網
上載者:User
這篇文章主要介紹了PHP利用cookies實現購物車的方法,可通過cookie實現對商品的增刪改等功能,以及統計與檢查等技巧,非常具有實用價值,需要的朋友可以參考下。
PHP購物車是在電子商務網站會用到的,一種像超市購物車一樣的,選好商品了,先放到自己的購物車裡面等好了再到櫃檯結算,本款PHP購物車完全按照這個原理來執行個體的,感興趣的朋友可以來看看,該執行個體利用了cookie來實現,代碼如下:
代碼如下:

<?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 = strips教程lashes($_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;  } } ?>

作為PHP開發程式員,掌握一種實現購物車的技能也是一項不錯的技能,結合前一章內容教程,希望大家對如何?購物車功能有更深的體會。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.