动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 一个比较完善的购物车类

作者来源: 
阅读 1207 人次 , 2006-3-29 4:10:00 


前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用。你可以简单的把这个类稍微修改一下就可以用在自己的程序里了,具体使用请见http://bigeagle.wotoo.com/article.asp?type=1

<?
/*****************************************************************************/
/*  */
/* 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[i] ;
 return $result;
}

//取总的商品种类数
function cartcount()
{
 return $this->sortcount;
}

//取总的商品价值
function gettotalcost()
{
 return $this->totalcost;
}
}

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:关于输出缓冲的讨论
· 下一篇:一个简单的cache示例
· PHP 编码规范(9)
· 第一节--面向对象编程 -- Classes and Objects in PHP5 [1]
· 做个自己站内搜索引擎
· PHP 编码规范(19)
· php输出控制类


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077