簡體   English   中英

PHP MySQL的二叉樹計算

[英]php mysql binary tree calculation

有四個層次的銷售經驗。 像A,B,C,D。

頂層的A和B始終在A之下。

只有D可以銷售產品,每次銷售可獲得10%的佣金。

如果直接在B下引入D,則B將獲得3.5%的佣金,而A將獲得1%的佣金。

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

如果直接在C下引入D,那么如果C在A下引入D,則C將獲得2%的佣金,而A將獲得2.5%的佣金。

如果直接在C下引入D,那么如果C在B下引入,那么C將獲得2%的佣金,B將獲得1.5%,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();

?>

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

您有項目路徑。 為什么不使用帶有路徑的assoc數組來找出佣金呢?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM