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的ob_start() 控制您的浏览器cache
Aug 03 PHP
php中计算程序运行时间的类代码
Nov 03 PHP
php上传apk后自动提取apk包信息的使用(示例下载)
Apr 26 PHP
zf框架的校验器InArray使用示例
Mar 13 PHP
跟我学Laravel之请求(Request)的生命周期
Oct 15 PHP
PHP获取POST数据的几种方法汇总
Mar 03 PHP
php写入数据到CSV文件的方法
Mar 14 PHP
mod_php、FastCGI、PHP-FPM等PHP运行方式对比
Jul 02 PHP
Yii2中使用join、joinwith多表关联查询
Jun 30 PHP
PHP实现的随机IP函数【国内IP段】
Jul 20 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
Nov 12 PHP
tp5框架前台无限极导航菜单类实现方法分析
Mar 29 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中文字符串截取函数
2013/11/12 PHP
php学习笔记之面向对象
2014/11/08 PHP
Yii中表单用法实例详解
2016/01/05 PHP
PHP数组去重比较快的实现方式
2016/01/19 PHP
Code:findPosX 和 findPosY
2006/12/20 Javascript
jquery HotKeys轻松搞定键盘事件代码
2008/08/30 Javascript
用js来定义浏览器中一个左右浮动元素相对于页面主体宽度的位置的函数
2012/01/21 Javascript
JS解析json数据并将json字符串转化为数组的实现方法
2012/12/25 Javascript
Javascript异步编程的4种方法让你写出更出色的程序
2013/01/17 Javascript
jQuery模拟超链接点击效果代码
2013/04/21 Javascript
文字不间断滚动(上下左右)实例代码
2013/04/21 Javascript
JavaScript变量声明详解
2014/11/27 Javascript
jQuery简单自定义图片轮播插件及用法示例
2016/11/21 Javascript
JavaScript版经典游戏之扫雷游戏完整示例【附demo源码下载】
2016/12/12 Javascript
JS实现双击内容变为可编辑状态
2017/03/03 Javascript
Angular2中select用法之设置默认值与事件详解
2017/05/07 Javascript
详解vue.js2.0父组件点击触发子组件方法
2017/05/10 Javascript
js实现移动端轮播图
2020/12/21 Javascript
jQuery AJAX与jQuery事件的分析讲解
2019/02/18 jQuery
vue中使用mxgraph的方法实例代码详解
2019/05/17 Javascript
python解析基于xml格式的日志文件
2017/02/25 Python
Python 私有函数的实例详解
2017/09/11 Python
python计算日期之间的放假日期
2018/06/05 Python
python 为什么说eval要慎用
2019/03/26 Python
关于 Python opencv 使用中的 ValueError: too many values to unpack
2019/06/28 Python
python字符串分割及字符串的一些常规方法
2019/07/24 Python
Python爬取爱奇艺电影信息代码实例
2019/11/26 Python
Python datetime 格式化 明天,昨天实例
2020/03/02 Python
解决python中import文件夹下面py文件报错问题
2020/06/01 Python
python raise的基本使用
2020/09/10 Python
python 指定源路径来解决import问题的操作
2021/03/04 Python
酒店节能降耗方案
2014/05/08 职场文书
化学教育专业求职信
2014/07/08 职场文书
严以修身专题学习研讨会发言材料
2015/11/09 职场文书
幼儿园开学家长寄语(2016秋季)
2015/12/03 职场文书
2016母亲节感恩话语
2015/12/09 职场文书