class Basket { var $basket_count; var $basket_item_id; var $basket_item_name; var $basket_item_quantity; var $basket_item_data; var $basket_item_price; function Basket() { $this->basket_count=0; } function Add_Item($ID,$name,$quantity=1,$price=0,$data='') { $this->basket_item_id[$this->basket_count]=$ID; $this->basket_item_name[$this->basket_count]=$name; $this->basket_item_quantity[$this->basket_count]=$quantity; $this->basket_item_data[$this->basket_count]=$data; $this->basket_item_price[$this->basket_count]=$price; $this->basket_count++; return ($this->basket_count-1); } function Del_Item($pos) { $this->basket_item_id[$pos]=''; } function Get_Item_ID($pos) { return $this->basket_item_id[$pos]; } function Get_Item_Name($pos) { return $this->basket_item_name[$pos]; } function Get_Item_Price($pos) { return $this->basket_item_price[$pos]; } function Get_Item_Quantity($pos) { return $this->basket_item_quantity[$pos]; } function Get_Item_Data($pos) { return $this->basket_item_data[$pos]; } function Set_Item_Quantity($pos,$quantity) { $this->basket_item_quantity[$pos]=$quantity; } function Set_Item_Data($pos,$data) { $this->basket_item_data[$pos]=$data; } function Enum_Items($start=false) { static $current; if ($current>=$this->basket_count) return -1; if (!$start) { $current++; } else { $current=0; } while (($this->basket_item_id[$current]=='') && ($current<$this->basket_count)) { $current++; } return ($current<$this->basket_count) ? $current : -1; } function Empty_Basket() { $this->basket_count=0; } function Get_Basket_Count() { $num=0; for ($i=0;$i<$this->basket_count;$i++) { if ($this->basket_item_id[$i]!='') $num++; } return $num; } } ?> session类中的一些方法使用: 注册一个session类 --------------------------------------- session_start(); if (! ssession_is_registered("basket") ) { $basket=new Basket; session_register("basket"); } --------------------------------------- 添加一件商品 ----------------------------------------------------------------- $basket->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data); ------------------------------------------------------------------ Del_* Get_* Set_* 删除商品 \ 得到商品值 \ 设置数量 (这只是几个例子,更多方法可以看类文件) ------------------------------------------------------------------- $basket->Del_Item(0) $basket->Get_Item_Data(0) $basket->Set_Item_Quantity(0,0) ------------------------------------------------------------------- 得到购物车所有的商品 ----------------------------------------------------------------------------- if ($basket->Get_Basket_Count()>0) { $pos = $basket->Enum_Items(true); while ($pos>=0) { print $basket->Get_Item_Name($pos)."-".$basket->Get_Item_Quantity($pos)."
"; $pos = $basket->Enum_Items(); } } -------------------------------------------------------------------------------------------------- 清空购物车 ----------------------------- $basket-> Empty_Basket(); -----------------------------