修改过的PHP购物车类

在使用Smarty调用购物车的时候,发现不能加入成功显示,研究了一天后,终于弄明白了。下面是修改后的购物车类
/**
 * Cart
 *
 * 购物车类
 *
 * @author  doodoo<
pWtitle@yahoo.com.cn>
 * @package     Cart
 * @category    Cart
 * @license     PHP License
 * @access      public
 * @version     $Revision: 1.20 $
 * @date  2004-08-21
 */
Class Cart{

 var $cart;
 var $totalCount; //商品总数量
 var $totalPrices; //商品总金额

  /**
   * Cart Constructor
   *
   * 类的构造函数,使购物车保持稳定的初始化状态
   *
   * @static
   * @access  public
   * @return  void   无返回值
   * @param   void   无参数
   */

  function Cart(){
  $this->totalCount = 0;
  $this->totalPrice = 0;
  $this->cart = array();
 }
 
 // }}}
 // {{{ add($item)

/**
 * 增加商品到当前购物车
 *
 * @access public
 * @param  array $item 商品信息(一维数组:array(商品ID,商品名称,商品单价,商品数量))
 * @return array   返回当前购物车内商品的数组
 */
 function add($item){
  if(!is_array($item)||is_null($item))   return $this->_getArray($this->cart);
  if(!is_numeric(end($item))||(!is_numeric(prev($item)))) {
   echo "价格和数量必须是数字";
   return $this->_getArray($this->cart);
  }
  reset($item); //这一句是必须的,因为上面的判断已经移动了数组的指标
  $key = current($item);
  if($key=="")   return $this->_getArray($this->cart);
  if($this->_isExists($key)){  //商品是否已经存在?
    $this->cart[$key]['count'] += end($item);
  return $this->_getArray($this->cart);
  }

  $this->cart[$key]['ID']  = $key;
  $this->cart[$key]['name'] = next($item);
  $this->cart[$key]['price'] = next($item);
  $this->cart[$key]['count'] = next($item);

 return $this->_getArray($this->cart);
 }

 // }}}
 // {{{ add($item)

/**
 * 从当前购物车中取出部分或全部商品
 * 当 $key=="" 的时候,清空当前购物车
 * 当 $key!=""&&$count=="" 的时候,从当前购物车中拣出商品ID号为 $key 的全部商品
 * 当 $key!=""&&$count!="" 的时候,从当前购物车中拣出 $count个 商品ID号为 $key 的商品
 *
 * @access public
 * @param  string $key 商品ID
 * @return mixed   返回真假或当前购物车内商品的数组
 */
 function remove($key="",$count=""){
  if($key=="") {
   $this->cart = array();
   return true;
  }
  if(!array_key_exists($key,$this->cart)) return false;
  if($count==""){ //移去这一类商品
   unset($this->cart[$key]);
  }else{ //移去$count个商品
   $this->cart[$key]['count'] -= $count;
   if($this->cart[$key]['count']<=0) unset($this->cart[$key]);
  }
  return $this->_getArray($this->cart);
 }

 // }}}
 // {{{ modi($key,$value)

/**
 * 修改购物车内商品ID为 $key 的商品的数量为 $value
 *
 * @access public
 * @param  string $key 商品ID
 * @param  int $value 商品数量
 * @return array  返回当前购物车内商品的数组;
 */
 function modi($key,$value){
  if(!$this->_isExists($key)) return $this->_getArray($this->cart); //不存在此商品,直接返回
  if($value<=0){     // value 太小,全部删除
   unset($this->cart[$key]);
   return $this->_getArray($this->cart);
  }
  $this->cart[$key]['count'] = $value;
  return $this->_getArray($this->cart);
 }


/**
 * 返回当前购物车内商品的数组
 *
 * @access public
 * @return array  返回当前购物车内商品的数组;
 */
 function getCart(){
  return $this->_getArray($this->cart);
 }

 // }}}
 // {{{ _isExists($key)

/**
 * 判断当前购物车中是否存在商品ID号为$key的商品
 *
 * @access private
 * @param  string $key 商品ID
 * @return bool   true or false;
 */
    function _isExists($key)
    {
  if(isset($this->cart[$key])&&!empty($this->cart[$key])&&array_key_exists($key,$this->cart))
   return true;
    return false;
    }

 // }}}
 // {{{ _getArray()

/**
 * 重新排列购物车内的产品,以便于smarty读取
 *
 * @access private
 * @return array  返回已经排列好的购物车内的商品;
 */
    function _getArray($array)
    {
 $myCart = array();
 foreach($array as $item)
  array_push($myCart,$item);
    return $myCart;
 }

 // }}}
 // {{{ isEmpty()

/**
 * 判断当前购物车是否为空,即没有任何商品
 *
 * @access public
 * @return bool   true or false;
 */
 function isEmpty(){
  return !count($this->cart);
 }

 // }}}
 // {{{ _stat()

/**
 * 取得部分统计信息
 *
 * @access private
 * @return bool  true or false;
 */

 function _stat(){
  if($this->isEmpty()) return false;
  foreach($this->cart as $item){
   $this->totalCount  += @end($item);
   $this->totalPrices += @prev($item);
  }
  return true;
 }

 // }}}
 // {{{ totalPrices()

/**
 * 取得当前购物车所有商品的总金额
 *
 * @access public
 * @return float  返回金额;
 */
 function totalPrices(){
  if($this->_stat())
   return $this->totalPrices;
 return 0;
 }

 // }}}
 // {{{ isEmpty()

/**
 * 取得当前购物车所有商品的总数量和
 *
 * @access public
 * @return int ;
 */
 function totalCount(){
  if($this->_stat())
   return $this->totalCount; 
 return 0;
 }


}//End Class Cart

session_start();
if(!isset($_SESSION['cart'])) {
 $_SESSION['cart'] = new Cart;
}
?>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值