PHP购物车类Cart.class.php定义与用法示例
本文实例讲述了PHP购物车类Cart.class.php定义与用法。分享给大家供大家参考,具体如下:
之前的开发人员使用了JS的技术开发了一套前台购物车(删除添加什么的都使用JS),但是浏览器兼容不好,今天终于出问题了,有个老外购物了产品,由于使用了不知名的浏览器,chrome,opera…都有可能,因此,我多了一份工作,重写购物车.
不打算再使用JS,直接考虑php.
找到了一个购物车的类,使用起来很方便.
Cart.class.php源码:
<?php
/**
*Cart
*
*购物车类
*
*@authordoodoo<pwtitle@yahoo.com.cn="">
*@packageCart
*@categoryCart
*@licensePHPLicense
*@accesspublic
*@version$Revision:1.10$
*/
ClassCart{
var$cart;
var$totalCount;//商品总数量
var$totalPrices;//商品总金额
/**
*CartConstructor
*
*类的构造函数,使购物车保持稳定的初始化状态
*
*@static
*@accesspublic
*@returnvoid无返回值
*@paramvoid无参数
*/
functionCart(){
$this->totalCount=0;
$this->totalPrice=0;
$this->cart=array();
}
//}}}
//{{{add($item)
/**
*增加商品到当前购物车
*
*@accesspublic
*@paramarray$item商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量))
*@returnarray返回当前购物车内商品的数组
*/
functionadd($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的商品
*
*@accesspublic
*@paramstring$key商品ID
*@returnmixed返回真假或当前购物车内商品的数组
*/
functionremove($key="",$count=""){
if($key==""){
$this->cart=array();
returntrue;
}
if(!array_key_exists($key,$this->cart))returnfalse;
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
*
*@accesspublic
*@paramstring$key商品ID
*@paramint$value商品数量
*@returnarray返回当前购物车内商品的数组;
*/
functionmodi($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;
}
/**
*返回当前购物车内商品的数组
*
*@accesspublic
*@returnarray返回当前购物车内商品的数组;
*/
functiongetCart(){
return$this->cart;
}
//}}}
//{{{_isExists($key)
/**
*判断当前购物车中是否存在商品ID号为$key的商品
*
*@accessprivate
*@paramstring$key商品ID
*@returnbooltrueorfalse;
*/
function_isExists($key)
{
if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart))
returntrue;
returnfalse;
}
//}}}
//{{{isEmpty()
/**
*判断当前购物车是否为空,即没有任何商品
*
*@accesspublic
*@returnbooltrueorfalse;
*/
functionisEmpty(){
return!count($this->cart);
}
//}}}
//{{{_stat()
/**
*取得部分统计信息
*
*@accessprivate
*@returnbooltrueorfalse;
*/
function_stat(){
if($this->isEmpty())returnfalse;
foreach($this->cartas$item){
$this->totalCount=@end($item);
$this->totalPrices=@prev($item);
}
returntrue;
}
//}}}
//{{{totalPrices()
/**
*取得当前购物车所有商品的总金额
*
*@accesspublic
*@returnfloat返回金额;
*/
functiontotalPrices(){
if($this->_stat())
return$this->totalPrices;
return0;
}
//}}}
//{{{isEmpty()
/**
*取得当前购物车所有商品的总数量和
*
*@accesspublic
*@returnint;
*/
functiontotalCount(){
if($this->_stat())
return$this->totalCount;
return0;
}
}//EndClassCart
?>
使用该类的方法:
<?php
header("Content-type:text/html;charset=utf8");
//调用实例
require_once'Cart.class.php';
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=newCart;
}
$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($itemsas$item){
echo"";
echo"<tableborder="1"><tbody><tr><formaction="\"index.php\""method="\"post\??=""></form><td>ID:".$item['ID']."<inputtype="hidden"value=".$item['ID']."name="key[]">";echo"</td><td>产品:".$item['name'];echo"</td><td>单价:".$item['price'];echo"</td><td><inputvalue=".$item['count']."name="value[]">";$sum=$item['count']*$item['price'];echo"</td><td>合计:".round($sum,2);echo"</td><td><inputonclick="\"location='?action=remove&key=".$item['ID']."'\""type="button"value="删除">";}echo"<inputtype="hidden"value="modi"name="action">";echo"</td></tr><tr><tdcolspan="7"><inputtype="submit"value="提交查询内容">";echo"</td></tr></tbody></table>";
?>
<hr>
<formaction="tmp.php"method="post">
ID:<inputname="p[]">
品名:<inputname="p[]">
单价:<inputname="p[]">
数量:<inputname="p[]">
<inputtype="hidden"value="add"name="action">
<inputtype="submit"value="提交查询内容">
</form></count>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+MySQL购物车开发专题》、《php面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。