溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何實(shí)現(xiàn)仿寫CodeIgniter的購(gòu)物車類

發(fā)布時(shí)間:2021-06-25 14:13:53 來(lái)源:億速云 閱讀:127 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹php如何實(shí)現(xiàn)仿寫CodeIgniter的購(gòu)物車類,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體如下:

這里仿寫CodeIgniter的購(gòu)物車類

購(gòu)物車基本功能:

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

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;
 //購(gòu)物車
 private $_cart_contents = array();
 /**
  * 構(gòu)造函數(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");
  }
 }
 /**
  * 將物品加入購(gòu)物車
  *
  * @access public
  * @param array 一維或多維數(shù)組,必須包含鍵值名: 
      id -> 物品ID標(biāo)識(shí), 
      qty -> 數(shù)量(quantity), 
      price -> 單價(jià)(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;
     }
    }
   }
  }
  //當(dāng)插入成功后保存數(shù)據(jù)到session
  if($save_cart) {
   $this->_save_cart();
   return TRUE;
  }
  return FALSE;
 }
 /**
  * 更新購(gòu)物車物品信息
  *
  * @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;
     }
    }
   }
  }
  //當(dāng)更新成功后保存數(shù)據(jù)到session
  if($save_cart) {
   $this->_save_cart();
   return TRUE;
  }
  return FALSE;
 }
 /**
  * 獲取購(gòu)物車物品總金額
  *
  * @return int
  */
 public function total() {
  return $this->_cart_contents['cart_total'];
 }
 /**
  * 獲取購(gòu)物車物品種類
  *
  * @return int
  */
 public function total_items() {
  return $this->_cart_contents['total_items'];
 }
 /**
  * 獲取購(gòu)物車
  *
  * @return array
  */
 public function contents() {
  return $this->_cart_contents;
 }
 /**
  * 獲取購(gòu)物車物品options
  *
  * @param string
  * @return array
  */
 public function options($rowid = '') {
  if($this->has_options($rowid)) {
   return $this->_cart_contents[$rowid]['options'];
  } else {
   return array();
  }
 }
 /**
  * 清空購(gòu)物車
  *
  */
 public function destroy() {
  unset($this->_cart_contents);
  $this->_cart_contents['cart_total'] = 0;
  $this->_cart_contents['total_items'] = 0;
  unset($_SESSION['cart_contents']);
 }
 /**
  * 判斷購(gòu)物車物品是否有options選項(xiàng)
  * 
  * @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ù)無(wú)效(無(wú)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ù)字,則我們對(duì)購(gòu)物車不做任何處理!
  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;
  }
  //去除物品單價(jià)左零及非數(shù)字(帶小數(shù)點(diǎn))字符
  $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
  $items['price'] = trim(preg_replace('/^([0]+)/i', '', $items['price']));
  //如果物品單價(jià)非數(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']);
  }
  //加入物品到購(gòu)物車
  unset($this->_cart_contents[$rowid]);
  $this->_cart_contents[$rowid]['rowid'] = $rowid;
  foreach($items as $key => $val) {
   $this->_cart_contents[$rowid][$key] = $val;
  }
  return TRUE;
 }
 /**
  * 更新購(gòu)物車物品信息(私有)
  *
  * @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ù)字,對(duì)購(gòu)物車不做任何處理!
  if( ! is_numeric($items['qty'])) {
   if($this->debug === TRUE) {
    $this->_log("cart_items_data(qty)_invalid");
   }
   return FALSE;
  }
  //如果購(gòu)物車物品數(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,表示不需要這件物品,從購(gòu)物車種清除
  //否則修改購(gòu)物車物品數(shù)量等于輸入的物品數(shù)量
  if($items['qty'] == 0) {
   unset($this->_cart_contents[$items['rowid']]);
  } else {
   $this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];
  }
  return TRUE;
 }
 /**
  * 保存購(gòu)物車數(shù)據(jù)到session
  * 
  * @access private
  * @return bool
  */
 private function _save_cart() {
  //首先清除購(gòu)物車總物品種類及總金額
  unset($this->_cart_contents['total_items']);
  unset($this->_cart_contents['cart_total']);
  //然后遍歷數(shù)組統(tǒng)計(jì)物品種類及總金額
  $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']);
  }
  //設(shè)置購(gòu)物車總物品種類及總金額
  $this->_cart_contents['total_items'] = count($this->_cart_contents);
  $this->_cart_contents['cart_total'] = $total;
  //如果購(gòu)物車的元素個(gè)數(shù)少于等于2,說(shuō)明購(gòu)物車為空
  if(count($this->_cart_contents) <= 2) {
   unset($_SESSION['cart_contents']);
   return FALSE;
  }
  //保存購(gòu)物車數(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如何實(shí)現(xiàn)仿寫CodeIgniter的購(gòu)物車類”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI