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模板引擎SMARTY
Oct 09 PHP
使用PHP数组实现无限分类,不使用数据库,不使用递归.
Dec 09 PHP
解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
Jun 24 PHP
解析:php调用MsSQL存储过程使用内置RETVAL获取过程中的return值
Jul 03 PHP
关于PHP语言构造器介绍
Jul 08 PHP
smarty模板中拼接字符串的方法
Feb 14 PHP
php防止伪造的数据从URL提交方法
Jun 27 PHP
PHP基于工厂模式实现的计算器实例
Jul 16 PHP
解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法
Dec 18 PHP
遍历指定目录,并存储目录内所有文件属性信息的php代码
Oct 28 PHP
laravel使用Faker数据填充的实现方法
Apr 12 PHP
Laravel开启跨域请求的方法
Oct 13 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文件处理类Dir.class.php的用法分析
2014/12/08 PHP
百度工程师讲PHP函数的实现原理及性能分析(二)
2015/05/13 PHP
thinkphp实现附件上传功能
2017/05/26 PHP
PHP实现的注册,登录及查询用户资料功能API接口示例
2017/06/06 PHP
javascript 限制输入脚本大全
2009/11/03 Javascript
jquery Tab效果和动态加载的简单实例
2013/12/11 Javascript
js字符串转换成数字与数字转换成字符串的实现方法
2014/01/08 Javascript
完美兼容各大浏览器的jQuery插件实现图片切换特效
2014/12/12 Javascript
用队列模拟jquery的动画算法实例
2015/01/20 Javascript
JS加载器如何动态加载外部js文件
2016/05/26 Javascript
Bootstrap实现水平排列的表单
2016/07/04 Javascript
详解vue2路由vue-router配置(懒加载)
2017/04/08 Javascript
深入理解Vue 单向数据流的原理
2017/11/09 Javascript
JavaScript canvas实现围绕旋转动画
2017/11/18 Javascript
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
浅谈Node.js爬虫之网页请求模块
2018/01/11 Javascript
JavaScript实现创建自定义对象的常用方式总结
2018/07/09 Javascript
vue搜索和vue模糊搜索代码实例
2019/05/07 Javascript
微信小程序自定义头部导航栏(组件化)
2019/11/15 Javascript
vue组件内部引入外部js文件的方法
2020/01/18 Javascript
Python类的基础入门知识
2008/11/24 Python
用Python的线程来解决生产者消费问题的示例
2015/04/02 Python
Python实现的购物车功能示例
2018/02/11 Python
Pandas之drop_duplicates:去除重复项方法
2018/04/18 Python
python  Django中的apps.py的目的是什么
2018/10/15 Python
解决import tensorflow as tf 出错的原因
2020/04/16 Python
Python爬取阿拉丁统计信息过程图解
2020/05/12 Python
video实现有声音自动播放的实现方法
2020/05/20 HTML / CSS
浅析图片上传及canvas压缩的流程
2020/06/10 HTML / CSS
来自美国主售篮球鞋的零售商店:KICKSUSA
2017/11/28 全球购物
班级标语大全
2014/06/21 职场文书
学校党的群众路线教育实践活动整改措施
2014/10/25 职场文书
务工证明怎么写
2015/06/18 职场文书
七年级作文之《我和我的祖国》观后感作文
2019/10/18 职场文书
SpringBoot整合MongoDB的实现步骤
2021/06/23 MongoDB
python 离散点图画法的实现
2022/04/01 Python