2014-01-30 3 views
2

세 가지 수준의 영업 emp가 있습니다. A, B, C, D.PHP mysql 이진 트리 계산

최고 수준의

A와 B처럼 항상 제품을 판매 할 수 A.

만 D에서 & 판매 당 10 %의 수수료를 얻을.

D가 B 아래에 직접 도입되면 B는 3.5 % 수수료를 받게됩니다 & A는 1 % 수수료를받습니다.

D가 A에 직접 도입되면 A는 판매 당 4.5 % 수수료를받습니다.

D는 C의 바로 아래에 도입되는 경우, C가 D는 C의 바로 아래에 유입되면 C는 A.하에 도입 된 경우는 2.5 % 임무를 얻을 것이다 수수료 & 2 %가

후, C는 2 % 수수료 &을 얻을 얻을

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`) 
); 
: B는 C가 직원 테이블 제작 한

B.

에서 도입 된 경우는 1 %의 수수료를 얻을 것이다 1.5 % &을 얻을 것이다

와 이진 트리 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(); 

?> 

모든 코드는 잘 작동하지만 난 모든 레벨 (자신과 부모)에 수수료를 계산하는 방법 개념을 정의 드릴 수 없습니다. 어떤 사람이 나를 도와 준다면.

+0

특정 사용자 계정의 "깊이"를 계산 한 다음 해당 특정 깊이에 대한 규칙을 찾아 적용합니다. 예 : 사용자 X는 깊이 4 (D 유형)이므로 D 유형 규칙을 적용하십시오. –

답변

0

항목 경로가 있습니다. 커미션을 알아 내기 위해 경로와 함께 assoc 배열을 사용하지 않는 이유는 무엇입니까?