使用PHP从SQL表构建html列表树

时间:2022-09-15 17:45:49

I have this table:

我有这个表:

+-------------------------------+
| NodeID | Parent | HasChildren |
+-------------------------------+
|1000000 |-1      |-1           |
+--------+--------+-------------+
|2409999 |1000000 |-1           |
+-------------------------------+
|2510921 |1000000 |-1           |
+-------------------------------+
|2596822 |2510921 |0            |
+-------------------------------+
|3000143 |2409999 |0            |
+-------------------------------+
|3125674 |2409999 |0            |
................................
       the list goes on

... from which I need to build a html tree list using <ul> and <li>. Every node in this table is a child of top node with id 1000000 (which has Parent "-1"). Also, HasChildren "-1" tells that this node has children, 0 - it has not. Yes, it's kinda weird convention, but it's as it is. So, the output should be like this:

…从中我需要使用

  • 建立一个html树列表。该表中的每个节点都是id为1000000(其父节点为“-1”)的top节点的子节点。同样,HasChildren“-1”告诉这个节点有子节点,0 -它没有子节点。是的,这是有点奇怪的惯例,但事实就是如此。输出应该是这样的:

  • 建立一个html树列表。该表中的每个节点都是id为1000000(其父节点为“1”)的前节点的子节点。同样,HasChildren“1”告诉这个节点有子节点,0 -它没有子节点。是的,这是有点奇怪的惯例,但事实就是如此。输出应该是这样的:
<ul>
  <li>2409999</li>
  <ul>
    <li>3000143</li>
    <li>3125674</li>
  </ul>
  <li>2510921</li>
  <ul>
    <li>2596822</li>
  </ul>
....
</ul>

Perhaps someone has tackled the very same problem? Any help would be appreciated. Thanks!

也许有人解决了同样的问题?如有任何帮助,我们将不胜感激。谢谢!

2 个解决方案

#1


3  

As mentioned above, it would make sense to use a nested set representation in your table. If you decide to keep the existing table structure, then the general way of doing it is to have a recursive function along the lines of:

如上所述,在表中使用嵌套集表示是有意义的。如果您决定保留现有的表结构,那么一般的方法是沿着以下几行设置一个递归函数:

function printBranch($parentID) {
    foreach ($children as $child) {
        if ($child is a Leaf) echo '<li>child</li>';
        elseif ($child is a Branch) printBranch($child);
    }
}

Needless to say the code above is pseudo-code, but it should demonstrate the general idea. The function executes on a single node, and if that node has children it calls itself on each of the children. That is called recursion. As a common programming convention: nodes that have children are called branches, and the ones that don't are called leafs.

毫无疑问,上面的代码是伪代码,但它应该能说明总体思想。该函数在单个节点上执行,如果该节点有子节点,它将在每个子节点上调用自己。这叫做递归。作为一种常见的编程约定:有子节点称为分支,而没有子节点称为leafs。

#2


0  

If your table layout is up for redesign, I'd use a left-right table layout, like the one listed in this article.

如果您的表布局是重新设计的,我将使用一个左右表布局,就像本文中列出的那样。

http://www.sitepoint.com/hierarchical-data-database-2/

http://www.sitepoint.com/hierarchical-data-database-2/

#1


3  

As mentioned above, it would make sense to use a nested set representation in your table. If you decide to keep the existing table structure, then the general way of doing it is to have a recursive function along the lines of:

如上所述,在表中使用嵌套集表示是有意义的。如果您决定保留现有的表结构,那么一般的方法是沿着以下几行设置一个递归函数:

function printBranch($parentID) {
    foreach ($children as $child) {
        if ($child is a Leaf) echo '<li>child</li>';
        elseif ($child is a Branch) printBranch($child);
    }
}

Needless to say the code above is pseudo-code, but it should demonstrate the general idea. The function executes on a single node, and if that node has children it calls itself on each of the children. That is called recursion. As a common programming convention: nodes that have children are called branches, and the ones that don't are called leafs.

毫无疑问,上面的代码是伪代码,但它应该能说明总体思想。该函数在单个节点上执行,如果该节点有子节点,它将在每个子节点上调用自己。这叫做递归。作为一种常见的编程约定:有子节点称为分支,而没有子节点称为leafs。

#2


0  

If your table layout is up for redesign, I'd use a left-right table layout, like the one listed in this article.

如果您的表布局是重新设计的,我将使用一个左右表布局,就像本文中列出的那样。

http://www.sitepoint.com/hierarchical-data-database-2/

http://www.sitepoint.com/hierarchical-data-database-2/