php利用cookies實(shí)現(xiàn)購(gòu)物車的方法
本文實(shí)例講述了php利用cookies實(shí)現(xiàn)購(gòu)物車的方法。分享給大家供大家參考。具體分析如下:
php購(gòu)物車是在電子商務(wù)網(wǎng)站會(huì)用到的,一種像超市購(gòu)物車一樣的,選好商品了,先放到自己的購(gòu)物車?yán)锩娴群昧嗽俚焦衽_(tái)結(jié)算,本款php購(gòu)物車完全按照這個(gè)原理來實(shí)例的,感興趣的朋友可以來看看,該實(shí)例利用了cookie來實(shí)現(xiàn),代碼如下:
/**
* 購(gòu)物車類 cookies 保存,保存周期為1天 注意:瀏覽器必須支持cookie才能夠使用
*/
class cartapi {
private $cartarray = array(); // 存放購(gòu)物車的二維數(shù)組
private $cartcount; // 統(tǒng)計(jì)購(gòu)物車數(shù)量
public $expires = 86400; // cookies過期時(shí)間,如果為0則不保存到本地 單位為秒
/**
* 構(gòu)造函數(shù) 初始化操作 如果$id不為空,則直接添加到購(gòu)物車
*
*/
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);
}
}
/**
* 添加商品到購(gòu)物車
*
* @param int $id 商品的編號(hào)
* @param string $name 商品名稱
* @param decimal $price1 商品價(jià)格
* @param decimal $price2 商品價(jià)格
* @param decimal $price3 商品價(jià)格
* @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)) { // 檢測(cè)商品是否存在
$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();
}
/**
* 修改購(gòu)物車?yán)锏纳唐?
*
* @param int $id 商品編號(hào)
* @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();
}
/**
* 清空購(gòu)物車
*
*/
public function removeall() {
$this->cartarray = array();
$this->save();
}
/**
* 查看購(gòu)物車信息
*
* @return array 返回一個(gè)二維數(shù)組
*/
public function cartview() {
$cookie = strips教程lashes($_cookie['cartapi']);
if (!$cookie) return false;
$tmpunserialize = unserialize($cookie);
return $tmpunserialize;
}
/**
* 檢查購(gòu)物車是否有商品
*
* @return bool 如果有商品,返回true,否則false
*/
public function checkcart() {
$tmparray = $this->cartview();
if (count($tmparray[0]) < 1) {
return false;
}
return true;
}
/**
* 商品統(tǒng)計(jì)
*
* @return array 返回一個(gè)一維數(shù)組 $arr[0]:產(chǎn)品1的總價(jià)格 $arr[1:產(chǎn)品2得總價(jià)格 $arr[2]:產(chǎn)品3的總價(jià)格 $arr[3]:產(chǎn)品的總數(shù)量
*/
public function countprice() {
$tmparray = $this->cartarray = $this->cartview();
$outarray = array(); //一維數(shù)組
// 0 是產(chǎn)品1的總價(jià)格
// 1 是產(chǎn)品2的總價(jià)格
// 2 是產(chǎn)品3的總價(jià)格
// 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)計(jì)商品數(shù)量
*
* @return int
*/
public function cartcount() {
$tmparray = $this->cartview();
$tmpcount = count($tmparray[0]);
$this->cartcount = $tmpcount;
return $tmpcount;
}
/**
* 保存商品 如果不使用構(gòu)造方法,此方法必須使用
*
*/
public function save() {
$tmparray = $this->cartarray;
$tmpserialize = serialize($tmparray);
setcookie("cartapi",$tmpserialize,time()+$this->expires);
}
/**
* 檢查購(gòu)物車商品是否存在
*
* @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;
}
}
?>
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- php實(shí)現(xiàn)保存周期為1天的購(gòu)物車類
- PHP購(gòu)物車類Cart.class.php定義與用法示例
- php實(shí)現(xiàn)仿寫CodeIgniter的購(gòu)物車類
- PHP實(shí)現(xiàn)的購(gòu)物車類實(shí)例
- PHP實(shí)現(xiàn)的比較完善的購(gòu)物車類
- php 購(gòu)物車完整實(shí)現(xiàn)代碼
- php 購(gòu)物車的例子
- 深入PHP購(gòu)物車模塊功能分析(函數(shù)講解,附源碼)
- php實(shí)現(xiàn)購(gòu)物車功能(以大蘋果購(gòu)物網(wǎng)為例)
- php+pdo實(shí)現(xiàn)的購(gòu)物車類完整示例
相關(guān)文章
php str_getcsv把字符串解析為數(shù)組的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp str_getcsv把字符串解析為數(shù)組的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04PHP 計(jì)算代碼執(zhí)行耗時(shí)的代碼修正網(wǎng)上普遍錯(cuò)誤
前幾天測(cè)試 SQLite 插入大數(shù)據(jù)量的時(shí)候, 找了一些關(guān)于計(jì)算執(zhí)行時(shí)間的代碼, 發(fā)現(xiàn)網(wǎng)上普遍流傳著這樣一份代碼2011-05-05在WordPress的后臺(tái)中添加頂級(jí)菜單和子菜單的函數(shù)詳解
這篇文章主要介紹了在WordPress的后臺(tái)中添加頂級(jí)菜單和子菜單的函數(shù)詳解,需要的朋友可以參考下2016-01-01PHP 獲取遠(yuǎn)程文件內(nèi)容的函數(shù)代碼
PHP 獲取遠(yuǎn)程文件內(nèi)容的代碼,后面有一些注釋可以參考下,其實(shí)大家可以參考腳本之家發(fā)布的一些采集程序代碼。2010-03-03PHP基于cookie與session統(tǒng)計(jì)網(wǎng)站訪問量并輸出顯示的方法
這篇文章主要介紹了PHP基于cookie與session統(tǒng)計(jì)網(wǎng)站訪問量并輸出顯示的方法,涉及PHP基于cookie與session讀寫操作記錄網(wǎng)站訪問量及調(diào)用圖片形式輸出對(duì)應(yīng)數(shù)量的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01php中目錄操作opendir()、readdir()及scandir()用法示例
這篇文章主要介紹了php中目錄操作opendir()、readdir()及scandir()用法,結(jié)合具體實(shí)例形式分析了PHP使用opendir()、readdir()及scandir()讀取目錄的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06微信公眾平臺(tái)開發(fā)教程②微信端分享功能圖文詳解
這篇文章主要介紹了微信公眾平臺(tái)開發(fā)微信端分享功能,結(jié)合圖文形式詳細(xì)分析了微信分享功能的原理、操作步驟及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04