选择显示字体大小

一个比较完善的购物车类


前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用。你可以简单的把这个类稍微修改一下就可以用在自己的程序里了,具体使用请见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(&quot;_cart_inc_&quot; , &quot;exists&quot;) ;

/*购物车类*/
class tcart
{

var &#36;sortcount; //商品种类数
var &#36;totalcost; //商品总价值

var &#36;id;  //每类商品的id(数组)
var &#36;name;  //每类商品的名称(数组)
var &#36;price; //每类商品的价格(数组)
var &#36;discount;  //商品的折扣(数组)
var &#36;goodprice ;  //商品的优惠价格(数组)
var &#36;count; //每类商品的件数(数组)
var &#36;maxcount ; //商品限量(数组)

//******构造函数
function tcart()
{
 &#36;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') ;

 &#36;this->update();
 &#36;this->calculate();
}

//********私有,根据session的值更新类中相应数据
function update()
{
global &#36;sid,&#36;sname,&#36;sprice,&#36;scount,&#36;sdiscount,&#36;smaxcount,&#36;sgoodprice;

 if(!isset(&#36;sid) or !isset(&#36;sname) or !isset(&#36;sprice)
or !isset(&#36;sdiscount) or !isset(&#36;smaxcount)
or !isset(&#36;sgoodprice) or !isset(&#36;scount)) return;

 &#36;this->id =&#36;sid;
 &#36;this->name =&#36;sname;
 &#36;this->price  =&#36;sprice;
 &#36;this->count  =&#36;scount;
 &#36;this->discount = &#36;sdiscount ;
 &#36;this->goodprice = &#36;sgoodprice ;
 &#36;this->maxcount = &#36;smaxcount ;

 //计算商品总数
 &#36;this->sortcount=count(&#36;sid);

}

//********私有,根据新的数据计算每类商品的价值及全部商品的总价
function calculate()
{
 for(&#36;i=0;&#36;i<&#36;this->sortcount;&#36;i++)
 {
 /*计算每件商品的价值,如果折扣是0 ,则为优惠价格*/
 &#36;giftprice = (&#36;this->discount[&#36;i] == 0 ? &#36;this->goodprice :
 ceil(&#36;this->price[&#36;i] * &#36;this->discount[&#36;i])/100 );
 &#36;this->totalcost += &#36;giftprice * &#36;this->count[&#36;i] ;
 }
}


//**************以下为接口函数

//*** 加一件商品
// 判断是否蓝中已有,如有,加count,否则加一个新商品
//首先都是改session的值,然后再调用update() and calculate()来更新成员变量
function add(&#36;a_id , &#36;a_name , &#36;a_price , &#36;a_discount ,
 &#36;a_goodprice , &#36;a_maxcount , &#36;a_count)
{
 global &#36;sid , &#36;sname , &#36;scount , &#36;sprice , &#36;sdiscount ,
&#36;sgoodprice , &#36;smaxcount ;

 &#36;k=count(&#36;sid);
 for (&#36;i=0; &#36;i<&#36;k; &#36;i++)
 { //先找一下是否已经加入了这种商品
 if(&#36;sid[&#36;i]==&#36;a_id)
 {
&#36;scount[&#36;i] += &#36;a_count ;
break;
 }
 }
 if(&#36;i >= &#36;k)
 { //没有则加一个新商品种类
&#36;sid[] = &#36;a_id;
&#36;sname[] = &#36;a_name;
&#36;sprice[]  = &#36;a_price;
&#36;scount[]  = &#36;a_count;
&#36;sgoodprice[] = &#36;a_goodprice ;
&#36;sdiscount[] = &#36;a_discount ;
&#36;smaxcount[] = &#36;a_maxcount ;
 }

 &#36;this->update(); //更新一下类的成员数据
 &#36;this->calculate();
}

//移去一件商品
function remove(&#36;a_id)
{
 global &#36;sid , &#36;sname , &#36;scount , &#36;sprice , &#36;sdiscount ,
&#36;sgoodprice , &#36;smaxcount ;

 &#36;k = count(&#36;sid);
 for(&#36;i=0; &#36;i < &#36;k; &#36;i++)
 {
 if(&#36;sid[&#36;i] == &#36;a_id)
 {
 &#36;scount[&#36;i] = 0 ;
 break;
 }
 }

 &#36;this->update();
 &#36;this->calculate();
}

//改变商品的个数
function modifycount(&#36;a_i,&#36;a_count)
{
 global &#36;scount;

 &#36;scount[&#36;a_i] = &#36;a_count ;
 &#36;this->update();
 &#36;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') ;
 &#36;this->sortcount = 0 ;
 &#36;this->totalcost = 0 ;
}


//是否某件商品已在蓝内,参数为此商品的id
function exists(&#36;a_id)
{
 for(&#36;i=0; &#36;i<&#36;this->sortcount; &#36;i++)
 {
 if(&#36;this->id[&#36;i]==&#36;a_id) return true;
 }
 return false;
}

//某件商品在蓝内的位置
function indexof(&#36;a_id)
{
 for(&#36;i=0; &#36;i<&#36;this->sortcount; &#36;i++)
 {
if(&#36;this->id[&#36;i]==&#36;id) return &#36;i;
 }
 return 0;
}

//取一件商品的信息,主要的工作函数
//返回一个关联数组,
function item(&#36;i)
{
 &#36;result[id] = &#36;this->id[&#36;i];
 &#36;result[name] = &#36;this->name[&#36;i];
 &#36;result[price]  = &#36;this->price[&#36;i];
 &#36;result[count]  = &#36;this->count[&#36;i];
 &#36;result[discount] = &#36;this->discount[&#36;i] ;
 &#36;result[goodprice] = &#36;this->goodprice[&#36;i] ;
 &#36;result[maxcount] = &#36;this->maxcount[i] ;
 return &#36;result;
}

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

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

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons