php实现仿写CodeIgniter的购物车类

 更新时间:2015年07月29日 16:51:59   作者:邪恶的小Y  
这篇文章主要介绍了php实现仿写CodeIgniter的购物车类,较为详细的分析了购物车的功能与具体实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • PHP获取二叉树镜像的方法

    PHP获取二叉树镜像的方法

    这篇文章主要介绍了PHP获取二叉树镜像的方法,涉及php使用队列针对二叉树进行翻转的相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • 解析PHP跨站刷票的实现代码

    解析PHP跨站刷票的实现代码

    本篇文章是对PHP跨站刷票的实现代码进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • 使用eAccelerator加密PHP程序

    使用eAccelerator加密PHP程序

    这款软件的功能就是通过在内存中缓存PHP文件从而加速PHP程序的执行速度。由于MMCache的原作者被Zend挖走,MMCache的开发一直处于停滞状态。而Eaccelerator却借鉴了MMCache的优点而一直在不断的更新。
    2008-10-10
  • php+mysql实现无限级分类 | 树型显示分类关系

    php+mysql实现无限级分类 | 树型显示分类关系

    php+mysql实现无限级分类 | 树型显示分类关系...
    2006-11-11
  • 一文带你搞懂PHP单例模式

    一文带你搞懂PHP单例模式

    单例就是单实例的意思,即在系统全局,一个类只创建一个对象,并且在系统全局都可以访问这个对象而不用重新创建。本文将通过示例为大家详细讲解Java单例模式的使用,需要的可以参考一下
    2022-12-12
  • 让PHP开发者事半功倍的十大技巧小结

    让PHP开发者事半功倍的十大技巧小结

    在PHP中,单纯按照自己思路去解决问题往往会是一种错误的办法。这并不是因为你是一个糟糕的程序员,而是因为如果你想写出好的可维护性强的代码,有些标准技巧是你必须要使用的。
    2010-04-04
  • 深入PHP中慎用双等于(==)的详解

    深入PHP中慎用双等于(==)的详解

    本篇文章是对PHP中慎用双等于(==)进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHP防注入安全代码

    PHP防注入安全代码

    判断传递的变量中是否含有非法字符我们把以下代码放到一个公共的文件里,比如security.inc.php里面,每个文件里都include一下这个文件,那么就能够给任何一个程序进行提交的所有变量进行过滤了,就达到了我们一劳永逸的效果。
    2008-04-04
  • php取出数组单个值的方法

    php取出数组单个值的方法

    下面小编就为大家分享一篇php取出数组单个值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • PHP 匿名函数与注意事项详细介绍

    PHP 匿名函数与注意事项详细介绍

    这篇文章主要介绍了PHP 匿名函数与注意事项详细介绍的相关资料,匿名函数是PHP5.3引进来了,php5.3不但引进了匿名函数还有更多更好多新的特性了,下面我们一起来了解一下PHP匿名函数与注意事项详解,需要的朋友可以参考下
    2016-11-11

最新评论