php mysql二叉树计算

时间:2014-01-30 15:21:13

标签: php mysql arrays traversal

有四个级别的销售人员。像A,B,C,D。

  
    

顶级A,B始终位于A。

         

只有D才能销售该产品&每次销售可获得10%的佣金。

         

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

         

如果D直接在A下引入,那么A将获得每销售4.5%的佣金。

         

如果直接在C下引入D,那么C将获得2%的佣金和如果在A下引入C,A将获得2.5%的佣金。

         

如果直接在C下引入D,那么C将获得2%的佣金和B将获得1.5%&如果在B下引入C,A将获得1%的佣金。

  

我为员工创建了表格:

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

?>

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

1 个答案:

答案 0 :(得分:0)

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