php专区

 首页 > php专区 > PHP应用 > php类库 > 一个比较完善的购物车类 - php类库

一个比较完善的购物车类 - php类库

分享到:
【字体:
导读:
          前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用,你可以简单的把这个类稍微修改一下就可以用在自己的程序里了...

一个比较完善的购物车类

前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用,你可以简单的把这个类稍微修改一下就可以用在自己的程序里了.

  1. /*****************************************************************************/ 
  2. /*                                                                           */ 
  3. /* file type:      包含文件,建议后缀为.inc                                  */ 
  4. /*                                                                           */ 
  5. /* file name:      cart.inc                                                  */ 
  6. /*                                                                           */ 
  7. /* Description:    定义一个购车类                                            */ 
  8. /*                                                                           */ 
  9. /* Func list :     class cart                                                */ 
  10. /*                                                                           */ 
  11. /* author :        bigeagle                                                  */ 
  12. /*                                                                           */ 
  13. /* date :          2000/12/24                                                */ 
  14. /*                                                                           */ 
  15. /* History:        2000/12/24  finished                                      */ 
  16. /*                                                                           */ 
  17. /*****************************************************************************/ 
  18.  
  19. //定义本文件常量 
  20. define("_CART_INC_" , "exists") ; 
  21.  
  22. /*购物车类*/ 
  23. class TCart 
  24.  
  25.   var $SortCount;            //商品种类数 
  26.   var $TotalCost;            //商品总价值 
  27.  
  28.   var $Id;                   //每类商品的ID(数组) 
  29.   var $Name;                 //每类商品的名称(数组) 
  30.   var $Price;                //每类商品的价格(数组) 
  31.   var $Discount;             //商品的折扣(数组) 
  32.   var $GoodPrice ;           //商品的优惠价格(数组) 
  33.   var $Count;                //每类商品的件数(数组) 
  34.   var $MaxCount ;            //商品限量(数组) 
  35.  
  36.   //******构造函数 
  37.   function TCart() 
  38.   { 
  39.    $this->SortCount=0; 
  40.  
  41.    session_start(); //初始化一个session 
  42.    session_register('sId'); 
  43.    session_register('sName'); 
  44.    session_register('sPrice'); 
  45.    session_register('sDiscount'); 
  46.    session_register('sGoodPrice') ; 
  47.    session_register('sCount') ; 
  48.    session_register('sMaxCount') ; 
  49.  
  50.    $this->Update(); 
  51.    $this->Calculate(); 
  52.   } 
  53.  
  54.   //********私有,根据session的值更新类中相应数据 
  55.   function Update() 
  56.   { 
  57.     global $sId,$sName,$sPrice,$sCount,$sDiscount,$sMaxCount,$sGoodPrice
  58.  
  59.    if(!isset($sIdor !isset($sNameor !isset($sPrice
  60.       or !isset($sDiscountor !isset($sMaxCount
  61.       or !isset($sGoodPriceor !isset($sCount)) return
  62.  
  63.    $this->Id        =$sId
  64.    $this->Name      =$sName
  65.    $this->Price     =$sPrice
  66.    $this->Count     =$sCount
  67.    $this->Discount  = $sDiscount ; 
  68.    $this->GoodPrice = $sGoodPrice ; 
  69.    $this->MaxCount  = $sMaxCount ; 
  70.  
  71.    //计算商品总数 
  72.    $this->SortCount=count($sId); 
  73.  
  74.   } 
  75.  
  76.   //********私有,根据新的数据计算每类商品的价值及全部商品的总价 
  77.   function Calculate() 
  78.   { 
  79.    for($i=0;$i<$this->SortCount;$i++) 
  80.    { 
  81.      /*计算每件商品的价值,如果折扣是0 ,则为优惠价格*/ 
  82.      $GiftPrice = ($this->Discount[$i] == 0 ? $this->GoodPrice : 
  83.                    ceil($this->Price[$i] * $this->Discount[$i])/100 ); 
  84.      $this->TotalCost += $GiftPrice * $this->Count[$i] ; 
  85.    } 
  86.   } 
  87.  
  88.  
  89.   //**************以下为接口函数 
  90.  
  91.   //*** 加一件商品 
  92.   // 判断是否蓝中已有,如有,加count,否则加一个新商品 
  93.   //首先都是改session的值,然后再调用update() and calculate()来更新成员变量 
  94.   function Add($a_ID , $a_Name , $a_Price , $a_Discount , 
  95.                $a_GoodPrice , $a_MaxCount , $a_Count
  96.   { 
  97.    global $sId , $sName , $sCount , $sPrice , $sDiscount , 
  98.           $sGoodPrice , $sMaxCount ; 
  99.  
  100.    $k=count($sId); 
  101.    for ($i=0; $i<$k$i++) 
  102.    { //先找一下是否已经加入了这种商品 
  103.      if($sId[$i]==$a_ID
  104.      { 
  105.       $sCount[$i] += $a_Count ; 
  106.       break
  107.      } 
  108.    } 
  109.    if($i >= $k
  110.    { //没有则加一个新商品种类 
  111.     $sId[]        = $a_ID
  112.     $sName[]      = $a_Name
  113.     $sPrice[]     = $a_Price
  114.     $sCount[]     = $a_Count
  115.     $sGoodPrice[] = $a_GoodPrice ; 
  116.     $sDiscount[]  = $a_Discount ; 
  117.     $sMaxCount[]  = $a_MaxCount ; 
  118.    } 
  119.  
  120.    $this->Update(); //更新一下类的成员数据 
  121.    $this->Calculate(); 
  122.   } 
  123.  
  124.   //移去一件商品 
  125.   function Remove($a_ID
  126.   { 
  127.    global $sId , $sName , $sCount , $sPrice , $sDiscount , 
  128.           $sGoodPrice , $sMaxCount ; 
  129.  
  130.    $k = count($sId); 
  131.    for($i=0; $i < $k$i++) 
  132.    { 
  133.      if($sId[$i] == $a_ID
  134.      { 
  135.        $sCount[$i] = 0 ; 
  136.        break
  137.      } 
  138.    } 
  139.  
  140.    $this->Update(); 
  141.    $this->Calculate(); 
  142.   } 
  143.  
  144.   //改变商品的个数 
  145.   function ModifyCount($a_i,$a_Count
  146.   { 
  147.    global $sCount
  148.  
  149.    $sCount[$a_i] = $a_Count ; 
  150.    $this->Update(); 
  151.    $this->Calculate(); 
  152.   } 
  153.  
  154.  
  155.   /*************************** 
  156.   清空所有的商品 
  157.   *****************************/ 
  158.   function RemoveAll() 
  159.   { 
  160.    session_unregister('sId'); 
  161.    session_unregister('sName'); 
  162.    session_unregister('sPrice'); 
  163.    session_unregister('sDiscount'); 
  164.    session_unregister('sGoodPrice') ; 
  165.    session_unregister('sCount') ; 
  166.    session_unregister('sMaxCount') ; 
  167.    $this->SortCount = 0 ; 
  168.    $this->TotalCost = 0 ; 
  169.   } 
  170.  
  171.  
  172.   //是否某件商品已在蓝内,参数为此商品的ID 
  173.   function Exists($a_ID
  174.   { 
  175.    for($i=0; $i<$this->SortCount; $i++) 
  176.    { 
  177.      if($this->Id[$i]==$a_IDreturn TRUE; 
  178.    } 
  179.    return FALSE; 
  180.   } 
  181.  
  182.   //某件商品在蓝内的位置 
  183.   function IndexOf($a_ID
  184.   { 
  185.    for($i=0; $i<$this->SortCount; $i++) 
  186.    { 
  187.     if($this->Id[$i]==$idreturn $i
  188.    } 
  189.    return 0; 
  190.   } 
  191.  
  192.   //取一件商品的信息,主要的工作函数 
  193.   //返回一个关联数组, 
  194.   function Item($i
  195.   { 
  196.    $Result[id]        = $this->Id[$i]; 
  197.    $Result[name]      = $this->Name[$i]; 
  198.    $Result[price]     = $this->Price[$i]; 
  199.    $Result[count]     = $this->Count[$i]; 
  200.    $Result[discount]  = $this->Discount[$i] ; 
  201.    $Result[goodprice] = $this->GoodPrice[$i] ; 
  202.    $Result[maxcount]  = $this->MaxCount[i] ; 
  203.    return $Result
  204.   } 
  205.  
  206.   //取总的商品种类数 
  207.   function CartCount() 
  208.   { 
  209.    return $this->SortCount; 
  210.   } 
  211.  
  212.   //取总的商品价值 
  213.   function GetTotalCost() 
  214.   { 
  215.    return $this->TotalCost; 
  216.   } 
  217. }  
  218. ?> 
分享到:
Codeigniter 购物车类不能添加中文解决方...
Codeigniter 购物车类不能添加中文解决方法 有朋友可能会发现Codeigniter 购物车类不能添加中文,我找了N久才发现下面一段代码限制了输入中文了,修改systemlibrariesCart.php,注释第186-190行产品名称的判断,代码如下: /*if ( ! preg_match("/^[".$this->product_name_rules."]+$/i...
php面象对象数据库操作类 - php类库
php面象对象数据库操作类 //*******************************************************************  //此处构造一个数据库操作类,封装所有数据库操作  //可以扩展便于后台管理程序的使用  Class MySQLDB   {      var $host;      var $user;   ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……