欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實現(xiàn)仿寫CodeIgniter的購物車類

 更新時間:2015年07月29日 16:51:59   作者:邪惡的小Y  
這篇文章主要介紹了php實現(xiàn)仿寫CodeIgniter的購物車類,較為詳細的分析了購物車的功能與具體實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

這里仿寫CodeIgniter的購物車類

購物車基本功能:

1) 將物品加入購物車
2) 從購物車中刪除物品
3) 更新購物車物品信息 【+1/-1】
4) 對購物車物品進行統(tǒng)計
   1. 總項目
   2. 總數(shù)量
   3. 總金額
5) 對購物單項物品的數(shù)量及金額進行統(tǒng)計
6) 清空購物車

cart.php文件如下:



<?php
/**
 *
 * @author quanshuidingdang
 */
class Cart {
 //物品id及名稱規(guī)則,調(diào)試信息控制
 private $product_id_rule = '\.a-z0-9-_'; //小寫字母 | 數(shù)字 | ._-
 private $product_name_rule = '\.\:a-z0-9-_';//小寫字母 | 數(shù)字 | ._-:
 private $debug = TRUE;
 //購物車
 private $_cart_contents = array();
 /**
  * 構造函數(shù)
  *
  * @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 一維或多維數(shù)組,必須包含鍵值名: 
      id -> 物品ID標識, 
      qty -> 數(shù)量(quantity), 
      price -> 單價(price), 
      name -> 物品姓名
  * @return bool
  */
 public function insert($items = array()) {
  //輸入物品參數(shù)異常
  if( ! is_array($items) OR count($items) == 0) {
   if($this->debug === TRUE) {
    $this->_log("cart_no_items_insert");
   }
   return FALSE;
  }
  //物品參數(shù)處理
  $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;
     }
    }
   }
  }
  //當插入成功后保存數(shù)據(jù)到session
  if($save_cart) {
   $this->_save_cart();
   return TRUE;
  }
  return FALSE;
 }
 /**
  * 更新購物車物品信息
  *
  * @access public
  * @param array
  * @return bool
  */
 public function update($items = array()) {
  //輸入物品參數(shù)異常
  if( !is_array($items) OR count($items) == 0) {
   if($this->debug === TRUE) {
    $this->_log("cart_no_items_insert");
   }
   return FALSE;
  }
  //物品參數(shù)處理
  $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;
     }
    }
   }
  }
  //當更新成功后保存數(shù)據(jù)到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;
 }
 /**
  * 插入數(shù)據(jù)
  *
  * @access private 
  * @param array
  * @return bool
  */
 private function _insert($items = array()) {
  //輸入物品參數(shù)異常
  if( ! is_array($items) OR count($items) == 0) {
   if($this->debug === TRUE) {
    $this->_log("cart_no_data_insert");
   }
   return FALSE;
  }
  //如果物品參數(shù)無效(無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;
  }
  //去除物品數(shù)量左零及非數(shù)字字符
  $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
  $items['qty'] = trim(preg_replace('/^([0]+)/i', '', $items['qty']));
  //如果物品數(shù)量為0,或非數(shù)字,則我們對購物車不做任何處理!
  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;
  }
  //去除物品單價左零及非數(shù)字(帶小數(shù)點)字符
  $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
  $items['price'] = trim(preg_replace('/^([0]+)/i', '', $items['price']));
  //如果物品單價非數(shù)字
  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()) {
  //輸入物品參數(shù)異常
  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;
  }
  //去除物品數(shù)量左零及非數(shù)字字符
  $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
  $items['qty'] = preg_replace('/^([0]+)/i', '', $items['qty']);
  //如果物品數(shù)量非數(shù)字,對購物車不做任何處理!
  if( ! is_numeric($items['qty'])) {
   if($this->debug === TRUE) {
    $this->_log("cart_items_data(qty)_invalid");
   }
   return FALSE;
  }
  //如果購物車物品數(shù)量與需要更新的物品數(shù)量一致,則不需要更新
  if($this->_cart_contents[$items['rowid']]['qty'] == $items['qty']) {
   if($this->debug === TRUE) {
    $this->_log("cart_items_data(qty)_equal");
   }
   return FALSE;
  }
  //如果需要更新的物品數(shù)量等于0,表示不需要這件物品,從購物車種清除
  //否則修改購物車物品數(shù)量等于輸入的物品數(shù)量
  if($items['qty'] == 0) {
   unset($this->_cart_contents[$items['rowid']]);
  } else {
   $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
  }
  return TRUE;
 }
 /**
  * 保存購物車數(shù)據(jù)到session
  * 
  * @access private
  * @return bool
  */
 private function _save_cart() {
  //首先清除購物車總物品種類及總金額
  unset($this->_cart_contents['total_items']);
  unset($this->_cart_contents['cart_total']);
  //然后遍歷數(shù)組統(tǒng)計物品種類及總金額
  $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;
  //如果購物車的元素個數(shù)少于等于2,說明購物車為空
  if(count($this->_cart_contents) <= 2) {
   unset($_SESSION['cart_contents']);
   return FALSE;
  }
  //保存購物車數(shù)據(jù)到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程序設計有所幫助。

相關文章

  • PHP獲取二叉樹鏡像的方法

    PHP獲取二叉樹鏡像的方法

    這篇文章主要介紹了PHP獲取二叉樹鏡像的方法,涉及php使用隊列針對二叉樹進行翻轉(zhuǎn)的相關操作技巧,需要的朋友可以參考下
    2018-01-01
  • 解析PHP跨站刷票的實現(xiàn)代碼

    解析PHP跨站刷票的實現(xiàn)代碼

    本篇文章是對PHP跨站刷票的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 使用eAccelerator加密PHP程序

    使用eAccelerator加密PHP程序

    這款軟件的功能就是通過在內(nèi)存中緩存PHP文件從而加速PHP程序的執(zhí)行速度。由于MMCache的原作者被Zend挖走,MMCache的開發(fā)一直處于停滯狀態(tài)。而Eaccelerator卻借鑒了MMCache的優(yōu)點而一直在不斷的更新。
    2008-10-10
  • php+mysql實現(xiàn)無限級分類 | 樹型顯示分類關系

    php+mysql實現(xiàn)無限級分類 | 樹型顯示分類關系

    php+mysql實現(xiàn)無限級分類 | 樹型顯示分類關系...
    2006-11-11
  • 一文帶你搞懂PHP單例模式

    一文帶你搞懂PHP單例模式

    單例就是單實例的意思,即在系統(tǒng)全局,一個類只創(chuàng)建一個對象,并且在系統(tǒng)全局都可以訪問這個對象而不用重新創(chuàng)建。本文將通過示例為大家詳細講解Java單例模式的使用,需要的可以參考一下
    2022-12-12
  • 讓PHP開發(fā)者事半功倍的十大技巧小結(jié)

    讓PHP開發(fā)者事半功倍的十大技巧小結(jié)

    在PHP中,單純按照自己思路去解決問題往往會是一種錯誤的辦法。這并不是因為你是一個糟糕的程序員,而是因為如果你想寫出好的可維護性強的代碼,有些標準技巧是你必須要使用的。
    2010-04-04
  • 深入PHP中慎用雙等于(==)的詳解

    深入PHP中慎用雙等于(==)的詳解

    本篇文章是對PHP中慎用雙等于(==)進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP防注入安全代碼

    PHP防注入安全代碼

    判斷傳遞的變量中是否含有非法字符我們把以下代碼放到一個公共的文件里,比如security.inc.php里面,每個文件里都include一下這個文件,那么就能夠給任何一個程序進行提交的所有變量進行過濾了,就達到了我們一勞永逸的效果。
    2008-04-04
  • php取出數(shù)組單個值的方法

    php取出數(shù)組單個值的方法

    下面小編就為大家分享一篇php取出數(shù)組單個值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • PHP 匿名函數(shù)與注意事項詳細介紹

    PHP 匿名函數(shù)與注意事項詳細介紹

    這篇文章主要介紹了PHP 匿名函數(shù)與注意事項詳細介紹的相關資料,匿名函數(shù)是PHP5.3引進來了,php5.3不但引進了匿名函數(shù)還有更多更好多新的特性了,下面我們一起來了解一下PHP匿名函數(shù)與注意事項詳解,需要的朋友可以參考下
    2016-11-11

最新評論