如何在PHP MySQL中查找任何节点的子节点?

时间:2019-03-14 04:42:51

标签: php mysql mlm

我有一个MLM二叉树,我想找到该用户的所有父级和子级。我该怎么办?

例如:

            A
       B         C
   D      E    F    G

基本上,我无法创建两个PHP MySQL函数,其中一个函数将返回所有孩子,而另一个将回溯所有父母和祖父母。

我的表结构是:

id | my_referral_id | referral_id

my_referral_id =我的推荐人ID; Referral_id =父母的推荐人ID

3 个答案:

答案 0 :(得分:0)

我对此有解决方案,

如果您在mysql中具有如下所示的树状结构,

id    |    parent_id
--------------------
1     |      0   
2     |      1
3     |      1
4     |      1
5     |      2
6     |      3
7     |      4

然后您可以像这样查询此表结构

$mysqli = mysqli_connect("127.0.0.1", "dbuser", "dbpass", "dbname");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

function get_children($id = 0, $mysqli) {    
    $ch = [];
    if($id==0){
        print_r($ch);
        return;
    }
    $children = get_children_loop($id, $ch, $mysqli);
    sort($children);
    return $children;
}

function get_children_loop($id, &$ch, $mysqli) {
    $query = 'select * from mlm where parent_id = ' . $id;
    $result = mysqli_query($mysqli, $query);    
    if ($result->num_rows) {
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
            $ch[] = $row['id'];
            get_children_loop($row['id'], $ch, $mysqli);
        }
    }
    return $ch;
}

function get_parents($id = 0, $mysqli) {

    $p = [];
    $parents = get_parents_loop($id, $p, $mysqli);
    sort($parents);
    return $parents;
}

function get_parents_loop($id, &$p, $mysqli) {
    $query = 'select * from mlm where id = ' . $id;
    $result = mysqli_query($mysqli, $query);
    if ($result->num_rows) {
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
            if ($row['parent_id']!= 0)
                $p[] = $row['parent_id'];
            get_parents_loop($row['parent_id'], $p, $mysqli);
        }
    }
    return $p;
}

$children = get_children(1, $mysqli);
print_r($children);
$parents = get_parents(6, $mysqli);
print_r($parents);

get_children($ id,$ mysqli)是一个具有ID的函数,并为您提供该ID的所有子代和孙代。

类似地, get_parents($ id,$ mysqli)为您提供所有具有特定ID的父母和祖父母。

$ mysqli是mysqli连接对象。

注意:-此代码段使用递归。

答案 1 :(得分:0)

为您工作。首先,您必须编写查询以获取所有行。其次,您可以使用递归函数创建父子节点。

 function1($parentId = null,&$listData) {
            $treeData = array();
            foreach ($listData as $key => $eachNodeData) {
                if ($eachNodeData['parent_id'] == $parentId) {
                    $eachNodeData['my_id'] = $this->createTree ($listData,$eachNodeData['id']);
                    $treeData[] = $eachNodeData;
                    unset($listData[$key]);
                }
            }
            return $treeData;
         }

答案 2 :(得分:0)

任何寻找CI版本的人都可以使用我的代码:

function _findParent($child_id, $parent_id)
{
    $parents = array();
    $direct_parent = $this->db->query("select parent_id from users where id='$child_id'")->row();
    if (!empty($direct_parent)) {
        array_push($parents, $direct_parent->parent_id);
        if (in_array($parent_id, $parents)) {
            return $parents;
        }
        $parents = $this->_findAllParents($direct_parent->parent_id, $parents, $parent_id);
    }
    return $parents;
}

function _findAllParents($child_id, &$parents, $parent_id)
{
    $direct_parent = $this->db->query("select parent_id from users where id='$child_id'")->row();
    if (!empty($direct_parent)) {
        array_push($parents, $direct_parent->parent_id);
        if (in_array($parent_id, $parents)) {
            return $parents;
        }
        $parents = $this->_findAllParents($direct_parent->parent_id, $parents, $parent_id);
    }
    return $parents;
}

function _findChild($parent_id, $child_id)
{
    $childs = array();
    $direct_childs = $this->db->query("select id from users where parent_id='$parent_id'")->result();
    if ($direct_childs != null) {
        foreach ($direct_childs as $ch) {
            array_push($childs, $ch->id);
            if (in_array($child_id, $childs)) {
                return $childs;
            }
            $childs = $this->_findAllChilds($ch->id, $childs, $child_id);
        }
    }
    return $childs;
}

function _findAllChilds($parent_id, &$childs, $child_id)
{
    $direct_childs = $this->db->query("select id from users where parent_id='$parent_id'")->result();
    if ($direct_childs != null) {
        foreach ($direct_childs as $ch) {
            array_push($childs, $ch->id);
            if (in_array($child_id, $childs)) {
                return $childs;
            }
            $this->_findAllChilds($ch->id, $childs, $child_id);
        }
    }
    return $childs;
}

,其用法是:

 // $children = $this->_findChild(1, 4);
 // log_message('ERROR', json_encode($children));
 // $parents = $this->_findParent(5, 2);
 // log_message('ERROR', json_encode($parents));
 // exit;

 $children = $this->_findChild($user_id, $parent_id->id); //pr,ch
 if (in_array($parent_id->id, $children)) {
     echo json_encode("The selected team leader $team_leader is already a child of $first_name $last_name...");
     exit;
 }

我希望您现在可以使用_findParent ...