一个购物车的类[1]

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

本文简介:选择自 kingerq 的 blog

 

<?php
class cart{
  var $sortcount; //商品种类数
  var $totalcost; //商品总金额
  /* 所有的商品,如:$mycart[3][$name]:商品编号为3的名称
  *               $mycart[3][$price]:商品编号为3的单价
  *          $mycart[3][$count]:商品编号为3的数量
  *               $mycart[3][$cost]:商品编号为3的合计金额
  */
  var $mycart    ; 
  var $id;        //每类商品的id(数组)
  var $name;        //每类商品的名称(数组)
  var $price;        //每类商品的价格(数组)
  var $count;        //每类商品的件数(数组)
  var $cost;        //每类商品的价值(数组)

  
  //******构造函数
  function cart(){
    $this->sortcount = 0    ;
    $this->totalcost = 0    ;
    $this->mycart    = array()    ;
    session_start();    //初始化一个session
    if(session_is_registered("mycart")==false)    session_register('mycart');      
    $this->update();
  //  $this->calculate();
    
  }
  
  //********私有,根据session的值更新类中相应数据
  function update(){
    session_start();    //初始化一个session
    $mycart = $_session["mycart"]        ;
    if(false==$mycart)
    {
        $this->sortcount = 0    ;
        $this->totalcost = 0    ;
        $this->mycart = array()    ;
        return false;
    }
    //得到商品的总数量
    $this->sortcount=count($mycart);
    if($this->sortcount>0)
    {
        //开始计算商品的金额
        $totalcost = 0    ;
        foreach($mycart as $key=>$val)
        {
            //先四舍五入
            foreach($val as $proname=>$proval)
            {
                if($proname !="name")
                {
                    $val[$proname] = round(eregi_replace(",", "",$proval),2)    ;
                    $mycart[$key][$proname] = $val[$proname]    ;
                }
            }
               
            //计算每件商品的金额
            $mycart[$key]["cost"] = round($val["count"]*$val["price"], 2)    ;
            //得到所有商品的金额
            $totalcost += $mycart[$key]["cost"]    ;           
        }
        $this->totalcost = $totalcost    ;
        $this->mycart = $mycart            ;
        $_session["mycart"] = $mycart    ;

    }
    
  }
  
 /**
 * 格式化数字为货币数据
 *
 *
 **/
  function formatnum($data)
  {
    foreach($data as $key=>$val)
    {

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

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

go top