2014-01-30 126 views
2

有四個級別的銷售額。如A,B,C,Dphp mysql二叉樹計算

頂級的A和B總是在A.

只有d可銷售的產品&得到每銷售10%的佣金。

如果D是直接在B下面引入,那麼B將獲得3.5%的佣金& A將獲得1%的佣金。

如果直接在A下引入D,則A將獲得每銷售4.5%的佣金。

如果d爲C下直接引入,那麼C將獲得2%的佣金& A將獲得2.5%的佣金如果C下A.介紹

如果d爲C下直接引入,那麼C將獲得2%的佣金& B會取得1.5%& A將獲得1%的佣金如果C B.

我創建表介紹了工作人員:

CREATE TABLE IF NOT EXISTS `parentchild` (
    `ID` bigint(20) NOT NULL AUTO_INCREMENT, 
    `Parent_ID` bigint(20) NOT NULL, 
    `Name` varchar(250) DEFAULT NULL, 
    `post` varchar(10) NOT NULL, 
    PRIMARY KEY (`ID`) 
); 

,並創建二叉樹parentchild.php代碼是:

<?php 
class ParentChild { 


    var $db_host; 
    var $db_user; 
    var $db_pass; 
    var $db_database; 
    var $db_table; 


    var $item_identifier_field_name; 
    var $parent_identifier_field_name; 
    var $item_list_field_name; 
    var $extra_condition=""; 
    var $order_by_phrase=""; 


    var $level_identifier = " "; 
    var $item_pointer = "|-"; 



    var $all_childs = array(); 
    var $item_path = array(); 
    public function getAllChilds($Parent_ID, $level_identifier="", $start=true) {  
     $immediate_childs=$this->getImmediateChilds($Parent_ID, $this->extra_condition, $this->order_by_phrase); 
     if(count($immediate_childs)) { 
      foreach($immediate_childs as $chld) { 
       $chld[$this->item_list_field_name]=$level_identifier.$this->item_pointer.$chld[$this->item_list_field_name]; 
       array_push($this->all_childs,$chld); 
       $this->getAllChilds($chld[$this->item_identifier_field_name], ($level_identifier.$this->level_identifier), false); 
      } 
     } 
     if($start) { 
      return $this->all_childs; 
     } 
    } 

    private function getImmediateChilds($parent_identifier_field_value, $extra_condition="", $order_by_phrase="") { 
     $sql="SELECT * FROM `".$this->db_table."` WHERE `".$this->parent_identifier_field_name."`='".$parent_identifier_field_value."' ".$extra_condition." ".$order_by_phrase; 
     $res=mysql_query($sql); 
     $childs=array(); 
     while($val=mysql_fetch_assoc($res)) { 
      array_push($childs,$val); 
     } 
     return $childs; 
    } 

    public function getItemPath($item_id,$start=true){ 

     if($item_id != 0) { 
      $sql="SELECT * FROM `".$this->db_table."` WHERE `".$this->item_identifier_field_name."`='".$item_id."' "; 
      $res=mysql_query($sql); 
      $itemdata=mysql_fetch_assoc($res); 
      array_push($this->item_path,$itemdata); 

      if($itemdata[$this->parent_identifier_field_name]!=0) { 
       $this->item_path=$this->getItemPath($itemdata[$this->parent_identifier_field_name],false); 
      } 
      if ($start) { 
       $this->item_path=array_reverse($this->item_path); 
      } 
     } 
     return $this->item_path; 

    } 

    public function db_connect(){ 
     $conn = mysql_connect($this->db_host, $this->db_user, $this->db_pass); 
     if($conn) { 
      mysql_select_db($this->db_database, $conn); 
     } 
     return $conn; 
    } 

    public function db_disconnect(){ 
     mysql_close(); 
    } 
} 
?> 


and example.php: 

<?php 

    require_once("ParentChild.php"); 

    $obj_parentchild = new ParentChild(); 

    $obj_parentchild->db_host="localhost"; 
    $obj_parentchild->db_user="root"; 
    $obj_parentchild->db_pass=""; 
    $obj_parentchild->db_database="test"; 

    if(!$obj_parentchild->db_connect()) { 
     echo "<h1>Sorry! Could not connect to the database server.</h1>"; 
     exit(); 
    } 

    $obj_parentchild->db_table="parentchild"; 
    $obj_parentchild->item_identifier_field_name="ID"; 
    $obj_parentchild->parent_identifier_field_name="Parent_ID"; 
    $obj_parentchild->item_list_field_name="Name"; 

    $obj_parentchild->extra_condition=""; 
    $obj_parentchild->order_by_phrase=" ORDER BY `ID` "; 

    $obj_parentchild->level_identifier=" "; 
    $obj_parentchild->item_pointer="->"; 




    $root_item_id=0; 
    $all_childs=$obj_parentchild->getAllChilds($root_item_id); 

    echo "<pre>"; 
    foreach($all_childs as $chld) { 
     echo $chld[$obj_parentchild->item_list_field_name]."<br />"; 
    } 



    echo "<p><b>Example : the full path for element q : </b></p>"; 
    $item_id=15; 
    $item_path_array=$obj_parentchild->getItemPath($item_id); 
    foreach ($item_path_array as $val) { echo $val['Name']."->"; } 

    $obj_parentchild->db_disconnect(); 

?> 

所有代碼都工作正常,但我無法定義的概念如何計算佣金各級(self和parent)。如果有人知道請幫助我。

+0

計算特定用戶帳戶的「深度」,然後查找該特定深度的規則,然後應用它們。用戶X是深度4(類型'D'),所以應用類型'D'規則... –

回答

0

您有項目路徑。爲什麼不使用assoc數組和路徑來計算佣金?