php mysql二叉树计算

php mysql二叉树计算,php,mysql,arrays,traversal,Php,Mysql,Arrays,Traversal,有四个级别的销售环境管理计划。比如A,B,C,D A为顶级,B始终在A之下 只有D可以销售产品,每次销售可获得10%的佣金 如果直接在B之下引入D,则B将获得3.5%的佣金,A将获得1%的佣金 如果直接在A项下引入D,则A每次销售将获得4.5%的佣金 如果D直接引入C,则C将获得2%的佣金;如果C引入A,则A将获得2.5%的佣金 如果D直接引入C,则C将获得2%的佣金,B将获得1.5%,A将获得1%的佣金,如果C引入B 我已经为员工创建了表: CREATE TABLE IF NOT EXISTS

有四个级别的销售环境管理计划。比如A,B,C,D

A为顶级,B始终在A之下

只有D可以销售产品,每次销售可获得10%的佣金

如果直接在B之下引入D,则B将获得3.5%的佣金,A将获得1%的佣金

如果直接在A项下引入D,则A每次销售将获得4.5%的佣金

如果D直接引入C,则C将获得2%的佣金;如果C引入A,则A将获得2.5%的佣金

如果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();

?>


所有代码都运行良好,但我无法定义如何计算各级(自己和家长)佣金的概念。如果有人知道,请帮助我。

您有项目路径。为什么不使用带有路径的assoc数组来计算佣金?

计算特定用户帐户的“深度”,然后查找特定深度的规则,然后应用它们。。。e、 g.用户X是深度4(类型
D
),因此应用类型
D
规则。。。