一个比较完善的购物车类
时间:2010-02-01
来源:互联网
<?
/*****************************************************************************/
/* */
/* file type: 包含文件,建议后缀为.inc */
/* */
/* file name: cart.inc */
/* */
/* Description: 定义一个购车类 */
/* */
/* Func list : class cart */
/* */
/* author : bigeagle */
/* */
/* date : 2000/12/24 */
/* */
/* History: 2000/12/24 finished */
/* */
/*****************************************************************************/
//定义本文件常量
define("_CART_INC_" , "exists") ;
/*购物车类*/
class TCart
{
var $SortCount; //商品种类数
var $TotalCost; //商品总价值
var $Id; //每类商品的ID(数组)
var $Name; //每类商品的名称(数组)
var $Price; //每类商品的价格(数组)
var $Discount; //商品的折扣(数组)
var $GoodPrice ; //商品的优惠价格(数组)
var $Count; //每类商品的件数(数组)
var $MaxCount ; //商品限量(数组)
//******构造函数
function TCart()
{
$this->SortCount=0;
session_start(); //初始化一个session
session_register('sId');
session_register('sName');
session_register('sPrice');
session_register('sDiscount');
session_register('sGoodPrice') ;
session_register('sCount') ;
session_register('sMaxCount') ;
$this->Update();
$this->Calculate();
}
//********私有,根据session的值更新类中相应数据
function Update()
{
global $sId,$sName,$sPrice,$sCount,$sDiscount,$sMaxCount,$sGoodPrice;
if(!isset($sId) or !isset($sName) or !isset($sPrice)
or !isset($sDiscount) or !isset($sMaxCount)
or !isset($sGoodPrice) or !isset($sCount)) return;
$this->Id =$sId;
$this->Name =$sName;
$this->Price =$sPrice;
$this->Count =$sCount;
$this->Discount = $sDiscount ;
$this->GoodPrice = $sGoodPrice ;
$this->MaxCount = $sMaxCount ;
//计算商品总数
$this->SortCount=count($sId);
}
//********私有,根据新的数据计算每类商品的价值及全部商品的总价
function Calculate()
{
for($i=0;$i<$this->SortCount;$i++)
{
/*计算每件商品的价值,如果折扣是0 ,则为优惠价格*/
$GiftPrice = ($this->Discount[$i] == 0 ? $this->GoodPrice :
ceil($this->Price[$i] * $this->Discount[$i])/100 );
$this->TotalCost += $GiftPrice * $this->Count[$i] ;
}
}
//**************以下为接口函数
//*** 加一件商品
// 判断是否蓝中已有,如有,加count,否则加一个新商品
//首先都是改session的值,然后再调用update() and calculate()来更新成员变量
function Add($a_ID , $a_Name , $a_Price , $a_Discount ,
$a_GoodPrice , $a_MaxCount , $a_Count)
{
global $sId , $sName , $sCount , $sPrice , $sDiscount ,
$sGoodPrice , $sMaxCount ;
$k=count($sId);
for ($i=0; $i<$k; $i++)
{ //先找一下是否已经加入了这种商品
if($sId[$i]==$a_ID)
{
$sCount[$i] += $a_Count ;
break;
}
}
if($i >= $k)
{ //没有则加一个新商品种类
$sId[] = $a_ID;
$sName[] = $a_Name;
$sPrice[] = $a_Price;
$sCount[] = $a_Count;
$sGoodPrice[] = $a_GoodPrice ;
$sDiscount[] = $a_Discount ;
$sMaxCount[] = $a_MaxCount ;
}
$this->Update(); //更新一下类的成员数据
$this->Calculate();
}
//移去一件商品
function Remove($a_ID)
{
global $sId , $sName , $sCount , $sPrice , $sDiscount ,
$sGoodPrice , $sMaxCount ;
$k = count($sId);
for($i=0; $i < $k; $i++)
{
if($sId[$i] == $a_ID)
{
$sCount[$i] = 0 ;
break;
}
}
$this->Update();
$this->Calculate();
}
//改变商品的个数
function ModifyCount($a_i,$a_Count)
{
global $sCount;
$sCount[$a_i] = $a_Count ;
$this->Update();
$this->Calculate();
}
/***************************
清空所有的商品
*****************************/
function RemoveAll()
{
session_unregister('sId');
session_unregister('sName');
session_unregister('sPrice');
session_unregister('sDiscount');
session_unregister('sGoodPrice') ;
session_unregister('sCount') ;
session_unregister('sMaxCount') ;
$this->SortCount = 0 ;
$this->TotalCost = 0 ;
}
//是否某件商品已在蓝内,参数为此商品的ID
function Exists($a_ID)
{
for($i=0; $i<$this->SortCount; $i++)
{
if($this->Id[$i]==$a_ID) return TRUE;
}
return FALSE;
}
//某件商品在蓝内的位置
function IndexOf($a_ID)
{
for($i=0; $i<$this->SortCount; $i++)
{
if($this->Id[$i]==$id) return $i;
}
return 0;
}
//取一件商品的信息,主要的工作函数
//返回一个关联数组,
function Item($i)
{
$Result[id] = $this->Id[$i];
$Result[name] = $this->Name[$i];
$Result[price] = $this->Price[$i];
$Result[count] = $this->Count[$i];
$Result[discount] = $this->Discount[$i] ;
$Result[goodprice] = $this->GoodPrice[$i] ;
$Result[maxcount] = $this->MaxCount ;
return $Result;
}
//取总的商品种类数
function CartCount()
{
return $this->SortCount;
}
//取总的商品价值
function GetTotalCost()
{
return $this->TotalCost;
}
}
作者: php华南培训 发布时间: 2010-02-01
// 购物车类 by MORR 2006-10-24 23:48
// Update by MORR 2007-03-21 10:39
/*
使用说明:
构造函数 cart 可以使用参数:
cart($cartname = 'myCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '')
$cartname 是购物车的标识,可以指定,可以保证不重名,不会有相关冲突
$session_id 是 session_id,默认是使用 cookie 来传输,也可以自定义,如果存储类型是 session 才起效
$savetype 存储类型,有 session 和 cookie 方式
... 其他是 cookie 需要的参数
如果程序本身也需要使用 session,建议购物车使用 cookie 存储
添加一个商品
============================================================
// 引用类
require_once './cart.class.php';
// 建立类实例
$cart = new cart();
// 商品已经存在 修改数据
if ($cart->data[$id]) {
$cart->data[$id]['count'] += $count;
$cart->data[$id]['money'] += $cart->data[$id]['price'] * $count;
// 添加商品
} else {
$cart->data[$id]['name'] = $name;
$cart->data[$id]['price'] = $price;
$cart->data[$id]['count'] = $count;
$cart->data[$id]['money'] = $price * $count;
}
// 保存购物车数据
$cart->save();
============================================================
编辑一个商品数量
============================================================
// 引用类
require_once './cart.class.php';
// 建立类实例
$cart = new cart();
// 商品已经存在 修改数据
if ($cart->data[$id]) {
$cart->data[$id]['count'] = $count;
$cart->data[$id]['money'] = $cart->data[$id]['price'] * $count;
// 保存购物车数据
$cart->save();
}
============================================================
删除一个商品
============================================================
// 引用类
require_once './cart.class.php';
// 建立类实例
$cart = new cart();
// 删除商品
unset($cart->data[$id]);
// 保存购物车数据
$cart->save();
============================================================
列表购物车
============================================================
// 引用类
require_once './cart.class.php';
// 建立类实例
$cart = new cart();
foreach ($cart->data AS $k => $v) {
echo '商品 ID: '.$k;
echo '商品名称: '.$v['name'];
echo '商品单价: '.$v['price'];
echo '商品数量: '.$v['count'];
echo '商品总价: '.$v['money'];
}
============================================================
某字段总累计 --- 如所有商品总价格
============================================================
// 引用类
require_once './cart.class.php';
// 建立类实例
$cart = new cart();
// 累计 money 字段
$cart->sum('money')
============================================================
清空购物车
============================================================
// 引用类
require_once './cart.class.php';
// 建立类实例
$cart = new cart();
// 清除数据
unset($cart->data);
// 保存购物车数据
$cart->save();
============================================================
*/
class cart {
// 购物车标识
var $cartname = '';
// 存储类型
var $savetype = '';
// 购物车中商品数据
var $data = array();
// Cookie 数据
var $cookietime = 0;
var $cookiepath = '/';
var $cookiedomain = '';
// 构造函数 (购物车标识, $session_id, 存储类型(session或cookie), 默认是一天时间, $cookiepath, $cookiedomain)
function cart($cartname = 'myCart', $session_id = '', $savetype = 'session', $cookietime = 86400, $cookiepath = '/', $cookiedomain = '') {
// 采用 session 存储
if ($savetype == 'session') {
if (!$session_id && $_COOKIE[$cartname.'_session_id']) {
session_id($_COOKIE[$cartname.'_session_id']);
} elseif($session_id)
session_id($session_id);
session_start();
if (!$session_id && !$_COOKIE[$cartname.'_session_id'])
setcookie($cartname.'_session_id', session_id(), $cookietime + time(), $cookiepath, $cookiedomain);
}
$this->cartname = $cartname;
$this->savetype = $savetype;
$this->cookietime = $cookietime;
$this->cookiepath = $cookiepath;
$this->cookiedomain = $cookiedomain;
$this->readdata();
}
// 读取数据
function readdata() {
if ($this->savetype == 'session') {
if ($_SESSION[$this->cartname] && is_array($_SESSION[$this->cartname]))
$this->data = $_SESSION[$this->cartname];
else
$this->data = array();
} elseif ($this->savetype == 'cookie') {
if ($_COOKIE[$this->cartname])
$this->data = unserialize($_COOKIE[$this->cartname]);
else
$this->data = array();
}
}
// 保存购物车数据
function save() {
if ($this->savetype == 'session') {
$_SESSION[$this->cartname] = $this->data;
} elseif ($this->savetype == 'cookie') {
if ($this->data)
setcookie($this->cartname, serialize($this->data), $this->cookietime + time(), $this->cookiepath, $this->cookiedomain);
}
}
// 返回商品某字段累加
function sum($field) {
$sum = 0;
if ($this->data)
foreach ($this->data AS $v)
if ($v[$field])
$sum += $v[$field] + 0;
return $sum;
}
}
?>
作者: php华南培训 发布时间: 2010-02-01
/*购物车类的例子
# Example:
# <?php
# include("sCart.php");
# $cart = new sCart();
#
# // register cart to session (optional)
# // session_start();
# // session_register("cart");
#
# // add a new product into the cart
# $cart->add_item(123, "Productname", 1, 6.95, 16, array("other descrīptions"));
#
# // show all products from the cart
# if($cart->show())
# {
# while($values = $cart->show_next_item())
# {
# print_r($values);
# }
# }
#
# // show sum of all products from the cart
# if($values = $cart->sum())
# {
# print_r($values);
# }
#
# // update an product of the cart
# $cart->update_item(123, 10);
#
# // remove an product of the cart
# $cart->remove_item(123);
#
# // clear complete cart
# $cart->clear();
# ?>
*/
if(!isset($_sCart_Included) || !$_sCart_Included)
{
$_sCart_Included = 1;
class sCartItem//添加一条商品
{
var $inumber;
var $name;
var $quant;
var $price;
var $taxqual;
var $descr;
//---相对于sCart多出来的变量
var $gprice;
var $nprice;
var $gtotprice;
var $ntotprice;
var $tax;
function sCartItem(&$inumber,&$name,&$quant,&$price,&$taxqual,&$descr)//构造函数形成一天纪录
{
$this->_init();
$this->inumber = $inumber;
$this->name = $name;
$this->quant = sprintf("%d", $quant);
$this->price = sprintf("%0.4f", $price);
$this->taxqual = sprintf("%d", $taxqual);
if(is_array($descr))//检测变量是否数组
{
foreach($descr as $key => $val)
{
$this->descr[$key] = $val;
}
}
$this->_calc();//利用传过来的值对这条数据进行计算
}
function update(&$quant)
{
$this->quant = $quant;
$this->_calc();
}
function get_ntotprice()
{
return $this->ntotprice;
}
function get_gtotprice()
{
return $this->gtotprice;
}
function get_tax()
{
return @array("taxqual" => $this->taxqual, "tax" => $this->tax);
}
function get_all()
{
$values = array();
$values["cart.inumber"] = $this->inumber;
$values["cart.name"] = $this->name;
$values["cart.quant"] = sprintf("%d", $this->quant);
$values["cart.price"] = sprintf("%0.2f", $this->price);
$values["cart.nprice"] = sprintf("%0.2f", $this->nprice);
$values["cart.gprice"] = sprintf("%0.2f", $this->gprice);
$values["cart.ntotprice"] = sprintf("%0.2f", $this->ntotprice);
$values["cart.gtotprice"] = sprintf("%0.2f", $this->gtotprice);
$values["cart.taxqual"] = $this->taxqual;
$values["cart.tax"] = sprintf("%0.2f", $this->tax);
foreach($this->descr as $key => $val)
{
$values["cart.descr.$key"] = $val;
}
return $values;
}
function _init()
{
$this->inumber = null;
$this->name = "";
$this->quant = 0;
$this->price = 0.00;
$this->descr = array();
$this->taxqual = 0;
$this->gprice = 0.00;
$this->nprice = 0.00;
$this->gtotprice = 0.00;
$this->ntotprice = 0.00;
$this->tax = 0.00;
}
function _calc()
{
$this->gprice = sprintf("%0.4f", $this->price - $this->discqual);
$this->tax = sprintf("%0.2f", ($this->gprice * $this->taxqual / (100 + $this->taxqual)) * $this->quant);
$this->nprice = sprintf("%0.4f", $this->gprice - ($this->gprice * $this->taxqual / (100 + $this->taxqual)));
$this->ntotprice = sprintf("%0.4f", $this->nprice * $this->quant);
$this->gtotprice = sprintf("%0.4f", $this->gprice * $this->quant);
}
}
//购物车类
class sCart
{
var $items;//篮子
var $gtotprice;
var $ntotprice;
var $tax;
function sCart()//sCart类的构造函数
{
$this->_init();
}
function add_item($inumber,$name,$quant,$price,$taxqual,$descr=0)
{
if(!is_object($this->items[$inumber]))//检查变量是否是一个变量
{
$this->items[$inumber] = new sCartItem($inumber,$name,$quant,$price,$taxqual,$descr);//123编号做购物篮的商品的编号
$this->_refresh();
}
}
function update_item($inumber,$quant)
{
if(is_object($this->items[$inumber]))
{
$this->items[$inumber]->update($quant);
$this->_refresh();
}
}
function remove_item($inumber)
{
if(is_object($this->items[$inumber]))
{
unset($this->items[$inumber]);
$this->_refresh();
}
}
function clear()
{
unset($this->items);
$this->_init();
}
function show()
{
reset($this->items);
return count($this->items);//货物的数量
}
function show_next_item()
{
if($item = each($this->items))
{
return $item["value"]->get_all();
}
}
function sum()
{
if(count($this->items))
{
$values = array();
$values["cart.sum.ntotprice"] = sprintf("%0.2f", $this->ntotprice);
$values["cart.sum.gtotprice"] = sprintf("%0.2f", $this->gtotprice);
foreach($this->tax as $key => $val)
{
$values["cart.sum.tax.".$key] = sprintf("%0.2f", $val);
}
return $values;
}
return false;
}
function _init()
{
$this->items = array();
$this->gtotprice = 0.00;
$this->ntotprice = 0.00;
$this->tax = array();
}
function _refresh()
{
$this->gtotprice = 0.00;
$this->ntotprice = 0.00;
$this->tax = array();
$count = 0;
reset($this->items);//将array 的内部指针倒回到第一个单元并返回第一个数组单元的值,如果数组为空则返回 FALSE。
foreach($this->items as $key => $val)
{
$this->gtotprice += $val->get_gtotprice();//所有商品的总的价格//新的知识
$this->ntotprice += $val->get_ntotprice();
$tax = $val->get_tax();//返回一键值对数组二维数组
$this->tax[$tax["taxqual"]] += $tax["tax"];
}
reset($this->items);
}
}
}
?>
作者: php华南培训 发布时间: 2010-02-01
晚上回去好好看
作者: yjhappy 发布时间: 2010-02-01

作者: save95 发布时间: 2010-02-01
作者: nevermore 发布时间: 2010-02-01
作者: moro 发布时间: 2010-02-01
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28