简体   繁体   English

PHP和MySQL在数据库中找到父母的所有子孙

[英]PHP and MySQL find all children and grandchildren of parents in database

I'm trying to achieve something like the following: Get all child, grandchild etc nodes under parent using php with mysql query results 我正在尝试实现以下目标: 使用带有mysql查询结果的php获取父节点下的所有子节点,孙子节点

But I'm looking to do it without putting everything in an array and without nesting if statments - just go through the first level one by one and: - print the item - if the item has a child find it and print it - if the child has a child find it and print it - if the child has no child move up one level and look for the next child or item and it's childs - and so on and so on ... preferably into a UL LI set of lists and sublists 但我希望做到这一点,而不是将所有内容都放入数组中,也不要嵌套语句-只需逐一级进行以下操作即可:-打印该项目-如果该项目有孩子,则将其打印并打印-如果孩子有一个孩子找到并打印-如果孩子没有一个孩子则向上移动并寻找下一个孩子或下一个孩子及其孩子-依此类推...子清单

My database and the output also needs to be ordered so it looks more like this: 我的数据库和输出也需要排序,因此看起来更像这样:

id     name       parent_id     orderby
1  Electronics          0         0
2  Televisions          1         10
3  Portable Electronics 1         20
4  Tube                 2         20
5  LCD                  2         10
6  Plasma               2         30
7  Mp3 Players          3         30
8  CD Players           3         20
9  2 Way Radios         3         10
10 Flash                7         10

I can do it using if statements but to do this I need to know how many levels the deepest child is and if there are, say, 7 levels that gets messy. 我可以使用if语句来做到这一点,但是要做到这一点,我需要知道最深子级的级别是多少,以及是否有7个级别混乱。 I've seen it (at the link above) put into a multidimensional array but then it seems that you need nested for each statements to get the data out again which is pretty much the same as the nested if solution. 我已经看到了它(在上面的链接中)放入多维数组中,但是看来您需要为每个语句嵌套以再次取出数据,这与嵌套的if解决方案几乎相同。

I'm pretty sure the answer is under my nose but haven't been able to find it here or elsewhere.... 我很确定答案在我的鼻子底下,但是在这里或其他地方找不到。

WordPress seems to have a way of doing it but I haven't been able to uncover the code there. WordPress似乎有办法做到这一点,但我一直无法在那里找到代码。

Any help would be much appreciated! 任何帮助将非常感激!

Use the following code to get the data : 使用以下代码获取数据:

function getChildren($parent) {
    $query = "SELECT * FROM tableName WHERE parent_id = $parent";
    $result = mysql_query($query);
    $children = array();
    $i = 0;
    while($row = mysql_fetch_assoc($result)) {
        $children[$i] = array();
        $children[$i]['name'] = $row['name'];
        $children[$i]['children'] = getChildren($row['id']);
    $i++;
    }
return $children;
}

Call this function using 使用以下命令调用此函数

$finalResult = getChildren('*');

EDIT by James 詹姆斯编辑

Just to finish this answer, to print out the results into the list: 只需完成此答案,即可将结果打印到列表中:

<?php
    function printList($array = null) {
        if (count($array)) {
            echo "<ul>";

            foreach ($array as $item) {
                echo "<li>";
                echo $item['name'];
                if (count($item['children'])) {
                    printList($item['children']);
                }
                echo "</li>";
            }

            echo "</ul>";
        }
    }

    printList($finalResult);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM