一个购物车的类[3]

[入库:2005年8月19日] [更新:2007年3月25日]

本文简介:选择自 kingerq 的 blog

        //如果有传入单价,则一起更改单价
        if($price >0 ) $mycart[$id]["price"]=$price;

           $_session["mycart"] = $mycart    ;
        $this->update();
    }
  
  }
  
  //清空一种商品
  function emptyone($i)
  {
    session_start();    //初始化一个session
    $mycart = $_session["mycart"]        ;
    unset($mycart[$i])    ;
    if(count($mycart)==0)
    {
        $this->emptyall()    ;
    }else{
        $_session["mycart"] = $mycart        ;  
        $this->update();
    }
  }
  
  
  /***************************
  清空所有的商品
  
  因为在win里php不支持session_destroy()函数,所以这个清空函数不完善,
  只是把每种商品的个数置为0。
  如果是在linux下,可以直接用session_destroy()来做。
  *****************************/
  function emptyall()
  {
       session_start();    //初始化一个session
    $mycart = $_session["mycart"]        ;
   
    unset($mycart)    ;
    $_session["mycart"] = $mycart        ;  
    $this->update();
    
  }
  
  /**
  *  返回所有购物车中的数据
  *
  **/
  function getdata()
  {
      if($this->sortcount > 0)
      {
          return $this->mycart    ;
      }else{
          return array()    ;
      }
  }
  //取一件商品的信息,主要的工作函数
  //返回一个关联数组,下标分别对应 id,name,price,count,cost
  function getone($i){
    $data = $this->mycart[$i]        ;
    if(false==$data) return array()    ;

    $data["id"]    =    $i                ;
    return $data                        ;

  }
  
  //取总的商品种类数
  function getsortcount(){
    return $this->sortcount;
  }
  
  //取总的商品价值
  function gettotalcost(){
    return $this->totalcost;
  }
  
//end class 
}
//////////////////////////////////////////////////////////////
$ocart = new cart();
$ocart->addone('5',"mm",2.4);    //添加一件苹果
$ocart->addone('4',"gg",2.4);    //添加一件苹果
$mycart=$ocart->getdata();       //购物车中的数据
/*下面是调试用的*/
echo "<pre>";
print_r(array_change_key_case($mycart, case_upper));
echo "</pre>";
exit;
?>

本文关键:一个购物车的类
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top