新版购物车类,加入查找功能以及完善错误处理功能[2]

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

本文简介:选择自 twt326 的 blog

  $_session["mc_numkey"]=$tfield[0];
  }
   else
     {$this->errmessage="参数传输错误,请传入从0开始下标的数组!";
  return false;}
 }//setfield end

  //设置关键字 (唯一关键字,如id,数量列名称,金额列名称)
  function setkey($key,$numkey,$goldkey)
    {
   $this->f_key=$key;
   $this->f_goldkey=$goldkey;
   $this->f_numkey=$numkey;
   //session_start();
   $_session["mc_key"]=$key;
   $_session["mc_goldkey"]=$goldkey;
   $_session["mc_numkey"]=$numkey;
 }
 
  //清除错误信息
  function clearerr()
    {
    $this->errmessage="";
 }
 
  //清除购物篮
  function clear()
    {
   $mycart=$_session["mycart"];
   unset ($mycart);
   $_session["mycart"]=$mycart;
   session_unregister("mycart");
   session_unregister("mc_fields");
   session_unregister("mc_key");
   session_unregister("mc_goldkey");
   session_unregister("mc_numkey");
   $this->cart=$mycart;
   $this->sp_count=0;
   $this->sp_sumgold=0;
 }
 
 //功能方法/////////////////////////////////////////////////////////
 /*查找某件商品,给出 查找内容或内容数组  查找字段名或字段名数组 返回商品序号数组 找不到返回false
 黙认查找关键列  如果给的参数是数组,那么内容数组和字段名组必须对应,且数量相等
 */
 function find($sstr,$sfield="0")
   {
  //session_start();
  $skey=array();
  $sfstr=array();
  $mycart=$_session["mycart"];
  //统一参数
  if (is_array($sstr))
    {
      if (! is_array($sfield))
     {
       $this->errmessage="参数类型不一致!";
    return false;
     }
   $skey=$sfield;
   $sfstr=$sstr;
    }
  else
    {
      if (is_array($sfield))
     {
       $this->errmessage="参数类型不一致!";
    return false;
     }
      if ($sfield=="0")
     $skey[0]=$this->f_key;
   else
     $skey[0]=$sfield;
   $sfstr[0]=$sstr;
    }
  $rtarray=array();
  //查找
  $cnt=0;
  for ($i=0;$i<$this->sp_count;$i++)
    {$tflag=true;
       //数组关键字及内容对应查找
    for ($j=0;$j<count($skey);$j++)
      if ($mycart[$i][$skey[$j]] != $sfstr[$j]) $tflag=false;
     
   if ($tflag)
       $rtarray[$cnt++]=$i;
    }
   return $rtarray;
   }
 //添加商品 数组参数,以自定义的字段名为下标
 function addone($splist)
   {
     if ((! is_array($splist)) || ((count($splist)-1) != $this->f_num))
    {$this->errmessage="参数传输错误,请传入以自定义字段名为下标且元数个数吻合的数组!";
    return false;}
  //session_start();
  $mycart=$_session["mycart"];
  //判断商品是否已经存在,如果存在,则加上
  $tflag=true;
  for ($i=0;$i<$this->sp_count;$i++)
    { if ($mycart[$i][$this->f_key]==$splist[$this->f_key])
     { $tflag=false;
    //增加商品个数
    $mycart[$i][$this->f_numkey]+=$splist[$this->f_numkey];
    $i=$this->sp_count;
     }
    }
   //添加商品到列表中
   if ($tflag)
     { for ($i=0;$i<=$this->f_num;$i++)
                 $mycart[$this->sp_count][$this->fields[$i]]=$splist[$this->fields[$i]];
    $this->sp_count+=1;
     }
     $this->cart=$mycart;
     $_session["mycart"]=$mycart;
      $this->updatecart();
   }//add end 
  
  
 //删除一个商品 参数为商品在购物车内的序号
 function delone($id)
   {    //session_start();
            $mycart = $_session["mycart"] ;

本文关键:新版购物车类,加入查找功能以及完善错误处理功能
 

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

go top