亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 網站 > 建站經驗 > 正文

php實現仿寫CodeIgniter的購物車類

2024-04-25 20:40:20
字體:
來源:轉載
供稿:網友

本文實例講述了php實現仿寫CodeIgniter的購物車類。分享給大家供大家參考。具體如下:

這里仿寫CodeIgniter的購物車類

購物車基本功能:

1) 將物品加入購物車

2) 從購物車中刪除物品

3) 更新購物車物品信息 【+1/-1】

4) 對購物車物品進行統計

1. 總項目

2. 總數量

3. 總金額

5) 對購物單項物品的數量及金額進行統計

6) 清空購物車

cart.php文件如下:

<?php

/**

*

* @author quanshuidingdang

*/

class Cart {

//物品id及名稱規則,調試信息控制

private $product_id_rule = '/.a-z0-9-_'; //小寫字母 | 數字 | ._-

private $product_name_rule = '/./:a-z0-9-_';//小寫字母 | 數字 | ._-:

private $debug = TRUE;

//購物車

private $_cart_contents = array();

/**

* 構造函數

*

* @param array

*/

public function __construct() {

//是否第一次使用?

if(isset($_SESSION['cart_contents'])) {

$this->_cart_contents = $_SESSION['cart_contents'];

} else {

$this->_cart_contents['cart_total'] = 0;

$this->_cart_contents['total_items'] = 0;

}

if($this->debug === TRUE) {

//$this->_log("cart_create_success");

}

}

/**

* 將物品加入購物車

*

* @access public

* @param array 一維或多維數組,必須包含鍵值名:

id -> 物品ID標識,

qty -> 數量(quantity),

price -> 單價(price),

name -> 物品姓名

* @return bool

*/

public function insert($items = array()) {

//輸入物品參數異常

if( ! is_array($items) OR count($items) == 0) {

if($this->debug === TRUE) {

$this->_log("cart_no_items_insert");

}

return FALSE;

}

//物品參數處理

$save_cart = FALSE;

if(isset($items['id'])) {

if($this->_insert($items) === TRUE) {

$save_cart = TRUE;

}

} else {

foreach($items as $val) {

if(is_array($val) AND isset($val['id'])) {

if($this->_insert($val) == TRUE) {

$save_cart = TRUE;

}

}

}

}

//當插入成功后保存數據到session

if($save_cart) {

$this->_save_cart();

return TRUE;
}

return FALSE;

}

/**

* 更新購物車物品信息

*

* @access public

* @param array

* @return bool

*/

public function update($items = array()) {

//輸入物品參數異常

if( !is_array($items) OR count($items) == 0) {

if($this->debug === TRUE) {
$this->_log("cart_no_items_insert");

}

return FALSE;

}

//物品參數處理

$save_cart = FALSE;

if(isset($items['rowid']) AND isset($items['qty'])) {

if($this->_update($items) === TRUE) {

$save_cart = TRUE;

}

} else {

foreach($items as $val) {

if(is_array($val) AND isset($val['rowid']) AND isset($val['qty'])) {

if($this->_update($val) === TRUE) {

$save_cart = TRUE;

}

}

}

}

//當更新成功后保存數據到session

if($save_cart) {

$this->_save_cart();

return TRUE;

}

return FALSE;

}

/**

* 獲取購物車物品總金額

*

* @return int

*/

public function total() {

return $this->_cart_contents['cart_total'];

}

/**

* 獲取購物車物品種類

*

* @return int

*/

public function total_items() {

return $this->_cart_contents['total_items'];

}

/**

* 獲取購物車

*

* @return array

*/

public function contents() {

return $this->_cart_contents;

}

/**

* 獲取購物車物品options

*

* @param string

* @return array

*/

public function options($rowid = '') {

if($this->has_options($rowid)) {

return $this->_cart_contents[$rowid]['options'];

} else {

return array();

}

}

/**

* 清空購物車

*

*/

public function destroy() {

unset($this->_cart_contents);

$this->_cart_contents['cart_total'] = 0;

$this->_cart_contents['total_items'] = 0;

unset($_SESSION['cart_contents']);

}

/**

* 判斷購物車物品是否有options選項

*

* @param string

* @return bool

*/

private function has_options($rowid = '') {

if( ! isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']) === 0) {

return FALSE;

}

return TRUE;

}

/**

* 插入數據

*

* @access private

* @param array

* @return bool

*/

private function _insert($items = array()) {

//輸入物品參數異常

if( ! is_array($items) OR count($items) == 0) {

if($this->debug === TRUE) {

$this->_log("cart_no_data_insert");

}

return FALSE;

}

//如果物品參數無效(無id/qty/price/name)

if( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name'])) {

if($this->debug === TRUE) {

$this->_log("cart_items_data_invalid");

}

return FALSE;

}

//去除物品數量左零及非數字字符

$items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));

$items['qty'] = trim(preg_replace('/^([0]+)/i', '', $items['qty']));

//如果物品數量為0,或非數字,則我們對購物車不做任何處理!

if( ! is_numeric($items['qty']) OR $items['qty'] == 0) {

if($this->debug === TRUE) {

$this->_log("cart_items_data(qty)_invalid");

}

return FALSE;

}


//物品ID正則判斷

if( ! preg_match('/^['.$this->product_id_rule.']+$/i', $items['id'])) {

if($this->debug === TRUE) {

$this->_log("cart_items_data(id)_invalid");

}

return FALSE;

}

//物品名稱正則判斷

if( ! preg_match('/^['.$this->product_name_rule.']+$/i', $items['name'])) {

if($this->debug === TRUE) {

$this->_log("cart_items_data(name)_invalid");

}

return FALSE;

}

//去除物品單價左零及非數字(帶小數點)字符

$items['price'] = trim(preg_replace('/([^0-9/.])/i', '', $items['price']));

$items['price'] = trim(preg_replace('/^([0]+)/i', '', $items['price']));

//如果物品單價非數字

if( ! is_numeric($items['price'])) {

if($this->debug === TRUE) {

$this->_log("cart_items_data(price)_invalid");

}

return FALSE;

}

//生成物品的唯一id

if(isset($items['options']) AND count($items['options']) >0) {

$rowid = md5($items['id'].implode('', $items['options']));

} else {

$rowid = md5($items['id']);

}

//加入物品到購物車

unset($this->_cart_contents[$rowid]);

$this->_cart_contents[$rowid]['rowid'] = $rowid;

foreach($items as $key => $val) {

$this->_cart_contents[$rowid][$key] = $val;

}

return TRUE;

}

/**

* 更新購物車物品信息(私有)

*

* @access private

* @param array

* @return bool

*/

private function _update($items = array()) {

//輸入物品參數異常

if( ! isset($items['rowid']) OR ! isset($items['qty']) OR ! isset($this->_cart_contents[$items['rowid']])) {

if($this->debug == TRUE) {

$this->_log("cart_items_data_invalid");

}

return FALSE;

}

//去除物品數量左零及非數字字符

$items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);

$items['qty'] = preg_replace('/^([0]+)/i', '', $items['qty']);

//如果物品數量非數字,對購物車不做任何處理!

if( ! is_numeric($items['qty'])) {

if($this->debug === TRUE) {

$this->_log("cart_items_data(qty)_invalid");

}

return FALSE;

}

//如果購物車物品數量與需要更新的物品數量一致,則不需要更新

if($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) {

if($this->debug === TRUE) {

$this->_log("cart_items_data(qty)_equal");

}

return FALSE;

}

//如果需要更新的物品數量等于0,表示不需要這件物品,從購物車種清除

//否則修改購物車物品數量等于輸入的物品數量

if($items['qty'] == 0) {

unset($this->_cart_contents[$items['rowid']]);

} else {

$this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];

}

return TRUE;

}

/**

* 保存購物車數據到session

*

* @access private

* @return bool

*/

private function _save_cart() {

//首先清除購物車總物品種類及總金額

unset($this->_cart_contents['total_items']);

unset($this->_cart_contents['cart_total']);

//然后遍歷數組統計物品種類及總金額

$total = 0;

foreach($this->_cart_contents as $key => $val) {

if( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty'])) {

continue;

}

$total += ($val['price'] * $val['qty']);

//每種物品的總金額

$this->_cart_contents[$key]['subtotal'] = ($val['price'] * $val['qty']);

}

//設置購物車總物品種類及總金額

$this->_cart_contents['total_items'] = count($this->_cart_contents);

$this->_cart_contents['cart_total'] = $total;

//如果購物車的元素個數少于等于2,說明購物車為空

if(count($this->_cart_contents) <= 2) {

unset($_SESSION['cart_contents']);

return FALSE;

}

//保存購物車數據到session

$_SESSION['cart_contents'] = $this->_cart_contents;

return TRUE;

}

/**

* 日志記錄

*

* @access private

* @param string

* @return bool

*/

private function _log($msg) {

return @file_put_contents('cart_err.log', $msg, FILE_APPEND);

}

}

/*End of file cart.php*/

/*Location /htdocs/cart.php*/

cart_demo.php文件如下:

<?php

session_start();

require_once('cart.php');

$items = array(

0 => array(

'id' => 'sp001',

'qty' => 20,

'price' => '10.50',

'name' => 'a002',

'options' => array(

'made' => 'china',

'company' => 'bgi'

)

),

1 => array(

'id' => 'sp002',

'qty' => 1,

'price' => '3.50',

'name' => 'b002'

)

);

$arr = array(

'rowid' => '86dbb7cb58a667558b4bbb1f60330028',

'qty' => 21

);

$cart = new Cart();

$cart->insert($items);

//var_dump($cart->contents());

$cart->update($arr);

var_dump($cart->contents());

//$cart->destroy();

//var_dump($_SESSION['cart_contents']);

/*end of php*/

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品v片在线观看不卡| 久久久亚洲成人| 国内精品模特av私拍在线观看| 狠狠综合久久av一区二区小说| 欧美性理论片在线观看片免费| 97在线视频一区| 秋霞成人午夜鲁丝一区二区三区| 国产欧美精品va在线观看| 91精品国产99| 久久久久国产一区二区三区| 亚洲伊人一本大道中文字幕| 日韩中文字幕精品| 日韩欧美亚洲国产一区| 日韩亚洲欧美中文在线| 黄色成人av网| 国产精品美女无圣光视频| 国产精品视频色| 色综久久综合桃花网| 精品福利免费观看| 欧美成人自拍视频| 国产精品入口福利| 精品国内亚洲在观看18黄| 国产精品夜间视频香蕉| 欧美日韩一区二区免费在线观看| 亚洲黄色免费三级| 欧美成年人视频| 国产高清视频一区三区| 国产视频精品自拍| 中文字幕自拍vr一区二区三区| 日韩一级裸体免费视频| 亚洲成人激情小说| 亚洲摸下面视频| 日韩av色综合| 亚洲欧美日韩国产中文| 国产精品v日韩精品| 久久九九免费视频| 久久久久久久电影一区| 欧美大片免费观看在线观看网站推荐| 97精品在线观看| 亚洲国产一区自拍| 国语自产精品视频在线看一大j8| 久久久精品在线| 欧美人交a欧美精品| 中文字幕在线看视频国产欧美在线看完整| 欧美国产日韩二区| 久久久之久亚州精品露出| 亚洲自拍偷拍网址| 国产91网红主播在线观看| 伊人男人综合视频网| 青青久久av北条麻妃海外网| 日本久久久a级免费| 久久久久久久久久久成人| 国产精品男人爽免费视频1| 一本一本久久a久久精品牛牛影视| 亚洲天堂2020| 成人444kkkk在线观看| 欧美野外猛男的大粗鳮| 91国偷自产一区二区三区的观看方式| 91成品人片a无限观看| 久久久久久网站| 中文字幕视频在线免费欧美日韩综合在线看| 久久精品夜夜夜夜夜久久| 欧美激情a∨在线视频播放| 亚洲人午夜精品免费| 久久九九全国免费精品观看| 国产精品日韩在线一区| 亚洲aⅴ男人的天堂在线观看| 国产精品2018| 国产亚洲精品久久久久久牛牛| 黑人狂躁日本妞一区二区三区| 日韩精品中文字幕在线观看| 免费91麻豆精品国产自产在线观看| 国产欧洲精品视频| 亚洲第一中文字幕在线观看| 国产成人91久久精品| 国产精品福利网| 亚洲精品美女视频| 夜夜嗨av一区二区三区四区| 日本一区二三区好的精华液| 亚洲a在线观看| 少妇高潮久久77777| 亚洲最大成人在线| zzjj国产精品一区二区| 欧美中文在线字幕| 精品国产一区久久久| 国产精品狠色婷| 一本色道久久综合狠狠躁篇的优点| 亚洲成人性视频| 欧美激情videoshd| 国产日韩在线看| 国产视频久久网| 国内精品久久久久久久久| 精品爽片免费看久久| 日韩在线视频一区| 最近2019年中文视频免费在线观看| 亚洲国产精品久久久久秋霞不卡| 精品久久久久久久久久久久久久| 色综合伊人色综合网站| 韩国国内大量揄拍精品视频| 夜夜嗨av一区二区三区免费区| 欧美大人香蕉在线| 奇米一区二区三区四区久久| 日韩精品视频在线观看免费| 国产成人在线视频| 欧美孕妇与黑人孕交| 久久精品国产综合| 国产色婷婷国产综合在线理论片a| 国产精品亚洲自拍| 国产91网红主播在线观看| 欧美午夜片欧美片在线观看| 91po在线观看91精品国产性色| 一区二区欧美亚洲| 欧美第一黄网免费网站| 久久精品视频一| 亚洲奶大毛多的老太婆| 亚洲va欧美va国产综合剧情| 国产精品久久久久一区二区| 久久资源免费视频| 91精品国产成人| 国产97在线|日韩| 欧美激情欧美狂野欧美精品| 欧美日本高清视频| 日韩免费视频在线观看| 国产精品va在线播放我和闺蜜| 日韩精品在线观看一区| 亚洲人午夜精品免费| 国产视频欧美视频| 欧美日韩一区二区三区在线免费观看| 欧美精品aaa| 成人黄色午夜影院| 成人精品久久久| 欧美成年人视频网站| 精品久久久久久久久国产字幕| 国产精品揄拍500视频| 57pao国产精品一区| 久久久国产一区二区三区| 少妇高潮 亚洲精品| 九色91av视频| 亚洲在线第一页| 91精品国产成人www| 久久精品色欧美aⅴ一区二区| 不卡av在线播放| 国产精品入口夜色视频大尺度| 国产精品视频资源| 日韩激情片免费| 国语自产精品视频在线看| 欧美日韩在线视频一区| 亚洲福利精品在线| 国产精品高清在线观看| 国产精品女视频| 日韩国产高清污视频在线观看| 日韩欧美精品免费在线| 一本大道久久加勒比香蕉| 亚洲国产成人精品久久| 国产国语videosex另类| 日本久久久a级免费| 91免费视频国产| 亚洲最新av在线网站| 亚洲综合视频1区| 黑人欧美xxxx| 日韩成人在线播放| 欧美大学生性色视频| 国产福利精品av综合导导航| 日韩电视剧免费观看网站|