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 in_array 函数使用说明与in_array需要注意的地方说明
Apr 13 PHP
PHP 循环列出目录内容的函数代码
May 26 PHP
PHP下通过exec获得计算机的唯一标识[CPU,网卡 MAC地址]
Jun 09 PHP
解析isset与is_null的区别
Aug 09 PHP
php针对cookie操作的队列操作类实例
Dec 10 PHP
PHP程序中使用adodb连接不同数据库的代码实例
Dec 19 PHP
PHP在线书签系统分享
Jan 04 PHP
YII框架中使用memcache的方法详解
Aug 02 PHP
thinkphp5.0自定义验证规则使用方法
Nov 16 PHP
浅谈PHP中pack、unpack的详细用法
Mar 12 PHP
Laravel框架自定义验证过程实例分析
Feb 01 PHP
laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子
Nov 14 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
地摊中国 - 珍藏老照片
2020/08/18 杂记
ThinkPHP中使用Ueditor富文本编辑器
2015/09/02 PHP
Zend Framework教程之Zend_Controller_Plugin插件用法详解
2016/03/07 PHP
PHP封装的验证码工具类定义与用法示例
2018/08/22 PHP
PHP parse_ini_file函数的应用与扩展操作示例
2019/01/07 PHP
详解PHP素材图片上传、下载功能
2019/04/12 PHP
在JavaScript中实现命名空间
2006/11/23 Javascript
JavaScript访问样式表代码
2010/10/15 Javascript
在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
2011/06/02 Javascript
JavaScript职责链模式概述
2016/09/17 Javascript
AngularJS中的JSONP实例解析
2016/12/01 Javascript
es6+angular1.X+webpack 实现按路由功能打包项目的示例
2017/08/16 Javascript
解决Layui数据表格中checkbox位置不居中的方法
2018/08/15 Javascript
详解vue beforeRouteEnter 异步获取数据给实例问题
2019/08/09 Javascript
js实现带搜索功能的下拉框
2020/01/11 Javascript
阿望教你用vue写扫雷小游戏
2020/01/20 Javascript
推荐11个实用Python库
2015/01/23 Python
浅谈python新手中常见的疑惑及解答
2016/06/14 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
2020/04/18 Python
详解Python中pandas的安装操作说明(傻瓜版)
2019/04/08 Python
python抓取需要扫微信登陆页面
2019/04/29 Python
python 实现分组求和与分组累加求和代码
2020/05/18 Python
Python logging日志库空间不足问题解决
2020/09/14 Python
英国和世界各地预订便宜的酒店:LateRooms.com
2019/05/05 全球购物
农药学硕士毕业生自荐信
2013/09/25 职场文书
管理工程专业求职信
2014/08/10 职场文书
反腐倡廉警示教育活动心得体会
2014/09/04 职场文书
党员反对四风问题思想汇报
2014/09/12 职场文书
医药公司采购员岗位职责
2014/09/12 职场文书
销售经理工作失职检讨书
2014/10/24 职场文书
暑期社会实践证明书
2014/11/17 职场文书
爱心募捐感谢信
2015/01/22 职场文书
自荐信格式范文
2015/03/04 职场文书
写给消防战士们的一封慰问信
2019/10/07 职场文书
win10+anaconda安装yolov5的方法及问题解决方案
2021/04/29 Python
5人制售《绝地求生》游戏外挂获利500多万元 被判刑
2022/03/31 其他游戏