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 相关文章推荐
自动生成文章摘要的代码[PHP 版本]
Mar 20 PHP
国外比较好的几个的Php开源建站平台小结
Apr 22 PHP
7个超级实用的PHP代码片段
Jul 11 PHP
有关phpmailer的详细介绍及使用方法
Jan 28 PHP
php二维数组排序与默认自然排序的方法介绍
Apr 27 PHP
PHP根据IP判断地区名信息的示例代码
Mar 03 PHP
thinkphp验证码显示不出来的解决方法
Mar 29 PHP
为PHP5.4开启Zend OPCode缓存
Dec 26 PHP
php获取客户端电脑屏幕参数的方法
Jan 09 PHP
Nginx下配置codeigniter框架方法
Apr 07 PHP
使用php-timeit估计php函数的执行时间
Sep 06 PHP
TP5框架使用QueryList采集框架爬小说操作示例
Mar 26 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
PHP 用数组降低程序的时间复杂度
2009/12/04 PHP
利用yahoo汇率接口实现实时汇率转换示例 汇率转换器
2014/01/14 PHP
php中current、next与reset函数用法实例
2014/11/17 PHP
php树型类实例
2014/12/05 PHP
jquery对元素拖动排序示例
2014/01/16 Javascript
Nodejs进程管理模块forever详解
2014/06/01 NodeJs
jquery实现保存已选用户
2014/07/21 Javascript
jquery.idTabs 选项卡使用示例代码
2014/09/03 Javascript
javascript实现网页屏蔽Backspace事件,输入框不屏蔽
2015/07/21 Javascript
Javascript实现的简单右键菜单类
2015/09/23 Javascript
Bootstrap每天必学之模态框(Modal)插件
2016/04/26 Javascript
Vue.js学习之过滤器详解
2017/01/22 Javascript
js+html实现周岁年龄计算器
2019/06/25 Javascript
改进 JavaScript 和 Rust 的互操作性并深入认识 wasm-bindgen 组件
2019/07/13 Javascript
深入了解JavaScript 防抖和节流
2019/09/12 Javascript
layui 阻止图片上传的实例(before方法)
2019/09/26 Javascript
vue实现多个echarts根据屏幕大小变化而变化实例
2020/07/19 Javascript
python脚本实现统计日志文件中的ip访问次数代码分享
2014/08/06 Python
Python操作Redis之设置key的过期时间实例代码
2018/01/25 Python
关于django 数据库迁移(migrate)应该知道的一些事
2018/05/27 Python
python学习开发mock接口
2019/04/28 Python
tensorflow 保存模型和取出中间权重例子
2020/01/24 Python
python对XML文件的操作实现代码
2020/03/27 Python
python 如何引入协程和原理分析
2020/11/30 Python
Python 实现PS滤镜中的径向模糊特效
2020/12/03 Python
基于HTML5 Canvas:字符串,路径,背景,图片的详解
2013/05/09 HTML / CSS
BannerBuzz加拿大:在线定制横幅印刷、广告和标志
2020/03/10 全球购物
人力资源行政经理自我评价
2013/10/23 职场文书
高级护理专业毕业生推荐信
2013/12/25 职场文书
旅游业大学生创业计划书
2014/01/31 职场文书
中药学专业毕业生推荐信
2014/07/10 职场文书
医学专业毕业生推荐信
2014/07/12 职场文书
我的中国梦主题教育活动总结
2015/05/07 职场文书
社区党建工作总结2015
2015/05/13 职场文书
学生病假条范文
2015/08/17 职场文书
python opencv检测直线 cv2.HoughLinesP的实现
2021/06/18 Python