PHP购物车类Cart.class.php定义与用法示例


Posted in PHP onJuly 20, 2016

本文实例讲述了PHP购物车类Cart.class.php定义与用法。分享给大家供大家参考,具体如下:

之前的开发人员使用了JS的技术开发了一套前台购物车(删除添加什么的都使用JS),但是浏览器兼容不好, 今天终于出问题了, 有个老外购物了产品, 由于使用了不知名的浏览器, chrome, opera…都有可能, 因此, 我多了一份工作, 重写购物车.

不打算再使用JS, 直接考虑php.

找到了一个购物车的类, 使用起来很方便.

Cart.class.php源码:

<?php
/**
 * Cart
 *
 * 购物车类
 *
 * @author doodoo<pwtitle @yahoo.com.cn="">
 * @package  Cart
 * @category Cart
 * @license  PHP License
 * @access  public
 * @version  $Revision: 1.10 $
 */
Class Cart{
 var $cart;
 var $totalCount; //商品总数量
 var $totalPrices; //商品总金额
 /**
  * Cart Constructor
  *
  * 类的构造函数,使购物车保持稳定的初始化状态
  *
  * @static
  * @access public
  * @return void 无返回值
  * @param void 无参数
  */
 function Cart(){
 $this->totalCount = 0;
 $this->totalPrice = 0;
 $this->cart = array();
 }
 // }}}
 // {{{ add($item)
 /**
 * 增加商品到当前购物车
 *
 * @access public
 * @param array $item 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量))
 * @return array 返回当前购物车内商品的数组
 */
 function add($item){
 if(!is_array($item)||is_null($item)) return $this->cart;
 if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
 echo "价格和数量必须是数字";
 return $this->cart;
 }
 reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标
 $key = current($item);
 if($key=="") return $this->cart;
 if($this->_isExists($key)){ //商品是否已经存在?
 $this->cart[$key]['count'] = end($item);
 return $this->cart;
 }
 $this->cart[$key]['ID'] = $key;
 $this->cart[$key]['name'] = next($item);
 $this->cart[$key]['price'] = next($item);
 $this->cart[$key]['count'] = next($item);
 return $this->cart;
 }
 // }}}
 // {{{ add($item)
 /**
 * 从当前购物车中取出部分或全部商品
 * 当 $key=="" 的时候,清空当前购物车
 * 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品
 * 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品
 *
 * @access public
 * @param string $key 商品ID
 * @return mixed 返回真假或当前购物车内商品的数组
 */
 function remove($key="",$count=""){
 if($key=="") {
 $this->cart = array();
 return true;
 }
 if(!array_key_exists($key,$this->cart)) return false;
 if($count==""){ //移去这一类商品
 unset($this->cart[$key]);
 }else{ //移去$count个商品
 $this->cart[$key]['count'] -= $count;
 if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
 }
 return $this->cart;
 }
 // }}}
 // {{{ modi($key,$value)
 /**
 * 修改购物车内商品ID为 $key 的商品的数量为 $value
 *
 * @access public
 * @param string $key 商品ID
 * @param int $value 商品数量
 * @return array 返回当前购物车内商品的数组;
 */
 function modi($key,$value){
 if(!$this->_isExists($key)) return $this->cart(); //不存在此商品,直接返回
 if($value<=0){  // value 太小,全部删除
 unset($this->cart[$key]);
 return $this->cart;
 }
 $this->cart[$key]['count'] = $value;
 return $this->cart;
 }
 /**
 * 返回当前购物车内商品的数组
 *
 * @access public
 * @return array 返回当前购物车内商品的数组;
 */
 function getCart(){
 return $this->cart;
 }
 // }}}
 // {{{ _isExists($key)
 /**
 * 判断当前购物车中是否存在商品ID号为$key的商品
 *
 * @access private
 * @param string $key 商品ID
 * @return bool true or false;
 */
 function _isExists($key)
 {
 if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart))
 return true;
 return false;
 }
 // }}}
 // {{{ isEmpty()
 /**
 * 判断当前购物车是否为空,即没有任何商品
 *
 * @access public
 * @return bool true or false;
 */
 function isEmpty(){
 return !count($this->cart);
 }
 // }}}
 // {{{ _stat()
 /**
 * 取得部分统计信息
 *
 * @access private
 * @return bool true or false;
 */
 function _stat(){
 if($this->isEmpty()) return false;
 foreach($this->cart as $item){
 $this->totalCount = @end($item);
 $this->totalPrices = @prev($item);
 }
 return true;
 }
 // }}}
 // {{{ totalPrices()
 /**
 * 取得当前购物车所有商品的总金额
 *
 * @access public
 * @return float 返回金额;
 */
 function totalPrices(){
 if($this->_stat())
 return $this->totalPrices;
 return 0;
 }
 // }}}
 // {{{ isEmpty()
 /**
 * 取得当前购物车所有商品的总数量和
 *
 * @access public
 * @return int ;
 */
 function totalCount(){
 if($this->_stat())
 return $this->totalCount;
 return 0;
 }
}//End Class Cart
?>

使用该类的方法:

<?php
header("Content-type:text/html;charset=utf8");
//调用实例
require_once 'Cart.class.php';
session_start();
if(!isset($_SESSION['cart'])) {
 $_SESSION['cart'] = new Cart;
}
$cart =& $_SESSION['cart'];
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='add') ){
 $p = $_POST['p'];
 $items = $cart->add($p);
}
if( ($_GET['action']=='remove')&&($_GET['key']!="") ) {
 $items = $cart->remove($_GET['key']);
}
if( ($_SERVER['REQUEST_METHOD']=="POST")&&($_POST['action']=='modi') ){
 $key = $_POST['key'];
 $value = $_POST['value'];
 for($i=0;$i<count ($key);$i="" $items="$cart-" ){="">modi($key[$i],$value[$i]);
 }
}
$items = $cart->getCart();
//打印
echo "";
setlocale(LC_MONETARY, 'it_IT');
foreach($items as $item){
 echo "";
 echo "<table border="1"><tbody><tr><form action="\"index.php\"" method="\" post\??=""></form><td>ID:".$item['ID']."<input type="hidden" value=".$item['ID']." name="key[]">"; echo "</td><td>产品:".$item['name']; echo "</td><td>单价:".$item['price']; echo "</td><td><input value=".$item['count']." name="value[]">"; $sum = $item['count']*$item['price']; echo "</td><td>合计:".round($sum,2); echo "</td><td><input onclick="\"location='?action=remove&key=".$item['ID']."'\"" type="button" value="删除">"; } echo "<input type="hidden" value="modi" name="action">"; echo "</td></tr><tr><td colspan="7"><input type="submit" value="提交查询内容">"; echo "</td></tr></tbody></table>";
?>
<hr>
<form action="tmp.php" method="post">
ID:<input name="p[]">
品名:<input name="p[]">
单价:<input name="p[]">
数量:<input name="p[]">
<input type="hidden" value="add" name="action">
<input type="submit" value="提交查询内容">
</form></count>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
模仿OSO的论坛(一)
Oct 09 PHP
php5中date()得出的时间为什么不是当前时间的解决方法
Jun 30 PHP
PHP计划任务、定时执行任务的实现代码
Apr 23 PHP
解析php安全性问题中的:Null 字符问题
Jun 21 PHP
解析func_num_args与func_get_args函数的使用
Jun 24 PHP
解析CI的AJAX分页 另类实现方法
Jun 27 PHP
thinkphp 一个页面使用2次分页的实现方法
Jul 15 PHP
PHP输出两个数字中间有多少个回文数的方法
Mar 23 PHP
php实现短信发送代码
Jul 05 PHP
session 加入redis的实现代码
Jul 15 PHP
php实现的简单数据库操作Model类
Nov 16 PHP
PHP 图片处理
Sep 16 PHP
无需数据库在线投票调查php代码
Jul 20 #PHP
thinkPHP2.1自定义标签库的导入方法详解
Jul 20 #PHP
php自定义函数实现二维数组排序功能
Jul 20 #PHP
Ajax提交表单时验证码自动验证 php后端验证码检测
Jul 20 #PHP
php使用get_class_methods()函数获取分类的方法
Jul 20 #PHP
PHP+Ajax验证码验证用户登录
Jul 20 #PHP
PHP+Ajax实现验证码的实时验证
Jul 20 #PHP
You might like
ThinkPHP 3.2 数据分页代码分享
2014/10/14 PHP
php利用gd库为图片添加水印
2016/11/09 PHP
php检查函数必传参数是否存在的实例详解
2017/08/28 PHP
JS 事件绑定函数代码
2010/04/28 Javascript
event.X和event.clientX的区别分析
2011/10/06 Javascript
js 取时间差去掉周六周日实现代码
2012/12/25 Javascript
js从10种颜色中随机取色实现每次取出不同的颜色
2013/10/23 Javascript
js实现宇宙星空背景效果的方法
2015/03/03 Javascript
jQuery实现3D文字特效的方法
2015/03/10 Javascript
JavaScript深度复制(deep clone)的实现方法
2016/02/19 Javascript
使用jQuery5分钟快速搞定双色表格的简单实例
2016/08/08 Javascript
每个程序员都需要学习 JavaScript 的7个理由小结
2016/09/03 Javascript
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
2016/12/15 Javascript
原生js实现拖拽功能基本思路详解
2018/04/18 Javascript
JavaScript创建对象的常用方式总结
2018/08/10 Javascript
Node.js Buffer模块功能及常用方法实例分析
2019/01/05 Javascript
微信小程序实现商品属性联动选择
2019/02/15 Javascript
vue分页器组件编写方法详解
2019/06/28 Javascript
JS开发 富文本编辑器TinyMCE详解
2019/07/19 Javascript
使用layui的layer组件做弹出层的例子
2019/09/27 Javascript
详解vue 自定义组件使用v-model 及探究其中原理
2019/10/11 Javascript
Python二分法搜索算法实例分析
2015/05/11 Python
Python实现针对中文排序的方法
2017/05/09 Python
python实现简单遗传算法
2018/03/19 Python
wxpython绘制音频效果
2019/11/18 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
2020/05/26 Python
Python偏函数Partial function使用方法实例详解
2020/06/17 Python
python输入中文的实例方法
2020/09/14 Python
运动会解说词200字
2014/02/06 职场文书
教育系统干部作风整顿心得体会
2014/09/09 职场文书
市场营销工作计划书
2014/09/15 职场文书
2014酒店客房部工作总结
2014/12/16 职场文书
先进员工事迹材料
2014/12/20 职场文书
学习习近平主席讲话心得体会
2016/01/20 职场文书
vue组件的路由高亮问题解决方法
2021/05/11 Vue.js
Python使用pandas导入xlsx格式的excel文件内容操作代码
2022/12/24 Python