[PHP] php购物车 session实现 →→→→→进入此内容的聊天室

来自 , 2020-05-23, 写在 PHP, 查看 170 次.
URL http://www.code666.cn/view/c0f971d8
  1.  
  2. class Basket {
  3.         var $basket_count;
  4.         var $basket_item_id;
  5.         var $basket_item_name;
  6.         var $basket_item_quantity;
  7.         var $basket_item_data;
  8.         var $basket_item_price;
  9.  
  10.         function Basket() {
  11.                 $this->basket_count=0;
  12.         }
  13.         function Add_Item($ID,$name,$quantity=1,$price=0,$data='') {
  14.                 $this->basket_item_id[$this->basket_count]=$ID;
  15.                 $this->basket_item_name[$this->basket_count]=$name;
  16.                 $this->basket_item_quantity[$this->basket_count]=$quantity;
  17.                 $this->basket_item_data[$this->basket_count]=$data;
  18.                 $this->basket_item_price[$this->basket_count]=$price;
  19.                 $this->basket_count++;
  20.                 return ($this->basket_count-1);
  21.         }
  22.         function Del_Item($pos) {
  23.                 $this->basket_item_id[$pos]='';
  24.         }
  25.         function Get_Item_ID($pos) {
  26.                 return $this->basket_item_id[$pos];
  27.         }
  28.         function Get_Item_Name($pos) {
  29.                 return $this->basket_item_name[$pos];
  30.         }
  31.         function Get_Item_Price($pos) {
  32.                 return $this->basket_item_price[$pos];
  33.         }
  34.         function Get_Item_Quantity($pos) {
  35.                 return $this->basket_item_quantity[$pos];
  36.         }
  37.         function Get_Item_Data($pos) {
  38.                 return $this->basket_item_data[$pos];
  39.         }
  40.         function Set_Item_Quantity($pos,$quantity) {
  41.                 $this->basket_item_quantity[$pos]=$quantity;
  42.         }
  43.         function Set_Item_Data($pos,$data) {
  44.                 $this->basket_item_data[$pos]=$data;
  45.         }
  46.         function Enum_Items($start=false) {
  47.                 static $current;
  48.                 if ($current>=$this->basket_count) return -1;
  49.                 if (!$start) {
  50.                         $current++;
  51.                 } else {
  52.                         $current=0;
  53.                 }
  54.                 while (($this->basket_item_id[$current]=='') && ($current<$this->basket_count)) {
  55.                         $current++;
  56.                 }
  57.                 return ($current<$this->basket_count) ? $current : -1;
  58.         }
  59.         function Empty_Basket() {
  60.                 $this->basket_count=0;
  61.         }
  62.         function Get_Basket_Count() {
  63.             $num=0;
  64.             for ($i=0;$i<$this->basket_count;$i++) {
  65.                         if ($this->basket_item_id[$i]!='') $num++;
  66.             }
  67.             return $num;
  68.         }
  69. }
  70. ?>
  71.  
  72.  
  73.  
  74.  
  75. session类中的一些方法使用:
  76.   注册一个session类
  77.   ---------------------------------------
  78.     session_start();
  79.    if (! ssession_is_registered("basket") ) {
  80.         $basket=new Basket;
  81.         session_register("basket");
  82.     }
  83.  
  84.    ---------------------------------------
  85.  
  86.   添加一件商品
  87.   -----------------------------------------------------------------
  88.  $basket->Add_Item($ITEM_ID,$DISPLAY_NAME,$quantity,$price,$data);
  89.   ------------------------------------------------------------------
  90.  
  91.  
  92.   Del_* Get_* Set_*  删除商品 \ 得到商品值 \ 设置数量 (这只是几个例子,更多方法可以看类文件)
  93.  -------------------------------------------------------------------
  94.   $basket->Del_Item(0)  
  95.   $basket->Get_Item_Data(0)
  96.   $basket->Set_Item_Quantity(0,0)
  97.  -------------------------------------------------------------------
  98.  
  99.  
  100.   得到购物车所有的商品
  101.   -----------------------------------------------------------------------------
  102.  
  103.   if ($basket->Get_Basket_Count()>0) {  
  104.         $pos = $basket->Enum_Items(true);
  105.         while ($pos>=0) {
  106.                 print $basket->Get_Item_Name($pos)."-".$basket->Get_Item_Quantity($pos)."<BR>";
  107.                 $pos = $basket->Enum_Items();
  108.            }
  109.       }  
  110.   --------------------------------------------------------------------------------------------------
  111.  
  112.    清空购物车
  113.  -----------------------------
  114.   $basket-> Empty_Basket();
  115.  -----------------------------

回复 "php购物车 session实现"

这儿你可以回复上面这条便签

captcha