php實現(xiàn)保存周期為1天的購物車類
更新時間:2017年07月07日 14:40:50 作者:楓羽靈~
這篇文章主要為大家詳細介紹了php實現(xiàn)保存周期為1天的購物車類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了php購物車類的具體代碼,供大家參考,具體內(nèi)容如下
購物車類 Cookies 保存,保存周期為1天 注意:瀏覽器必須支持Cookie才能夠使用
示例代碼:
<?php /** * 購物車類 Cookies 保存,保存周期為1天 注意:瀏覽器必須支持Cookie才能夠使用 */ class CartAPI { private $CartArray = array(); // 存放購物車的二維數(shù)組 private $CartCount; // 統(tǒng)計購物車數(shù)量 public $Expires = 86400; // Cookies過期時間,如果為0則不保存到本地 單位為秒 /** * 構造函數(shù) 初始化操作 如果$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 商品數(shù)量 * @param string $Image 商品圖片 * @return 如果商品存在,則在原來的數(shù)量上加1,并返回false */ public function addCart($Id,$Name,$Price1,$Price2,$Price3,$Count,$Image) { $this->CartArray = $this->CartView(); // 把數(shù)據(jù)讀取并寫入數(shù)組 if ($this->checkItem($Id)) { // 檢測商品是否存在 $this->ModifyCart($Id,$Count,0); // 商品數(shù)量加$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 商品數(shù)量 * @param int $Flag 修改類型 0:加 1:減 2:修改 3:清空 * @return 如果修改失敗,則返回false */ public function ModifyCart($Id, $Count, $Flag = "") { $tmpId = $Id; $this->CartArray = $this->CartView(); // 把數(shù)據(jù)讀取并寫入數(shù)組 $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: // 添加數(shù)量 一般$Count為1 $tmpArray[5][$Id] += $Count; break; case 1: // 減少數(shù)量 $tmpArray[5][$Id] -= $Count; break; case 2: // 修改數(shù)量 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 返回一個二維數(shù)組 */ 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; } /** * 商品統(tǒng)計 * * @return array 返回一個一維數(shù)組 $arr[0]:產(chǎn)品1的總價格 $arr[1:產(chǎn)品2得總價格 $arr[2]:產(chǎn)品3的總價格 $arr[3]:產(chǎn)品的總數(shù)量 */ public function CountPrice() { $tmpArray = $this->CartArray = $this->CartView(); $outArray = array(); //一維數(shù)組 // 0 是產(chǎn)品1的總價格 // 1 是產(chǎn)品2的總價格 // 2 是產(chǎn)品3的總價格 // 3 是產(chǎn)品的總數(shù)量 $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; } /** * 統(tǒng)計商品數(shù)量 * * @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; } } ?>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
PHP中substr()與explode()函數(shù)用法分析
這篇文章主要介紹了PHP中substr()與explode()函數(shù)用法分析,以實例的形式較為詳細的講述了substr()與explode()函數(shù)處理字符串的技巧,是字符串操作中使用頻率比較高的函數(shù),具有一定的實用價值,需要的朋友可以參考下2014-11-11