前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用,你可以简单的把这个类稍微修改一下就可以用在自己的程序里了.
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- define("_CART_INC_" , "exists") ;
-
-
- class TCart
- {
-
- var $SortCount;
- var $TotalCost;
-
- var $Id;
- var $Name;
- var $Price;
- var $Discount;
- var $GoodPrice ;
- var $Count;
- var $MaxCount ;
-
-
- function TCart()
- {
- $this->SortCount=0;
-
- session_start();
- 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();
- }
-
-
- 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] ;
- }
- }
-
-
-
-
-
-
-
- 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 ;
- }
-
-
-
- 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;
- }
- }
- ?>
(责任编辑:admin) |