N 层树 PHP MYSQL 帮助

发布于 2024-08-07 14:46:11 字数 813 浏览 6 评论 0原文

我一直在研究 N 级树类

任何人都可以建议我显示结果的最佳方式,即结果集的循环,我应该使用 DIV 还是 UL?

如果您发现文章太长而无法阅读,示例结果集将如下所示

Array ( [531] => stdClass Object ( [id] => 531 [rid] => 0 [created] => 1255384549 [nleft] => 1 [nright] => 8 [nlevel] => 1 ) [534] => stdClass Object ( [id] => 534 [rid] => 531 [created] => 1255384549 [nleft] => 2 [nright] => 3 [nlevel] => 2 ) [532] => stdClass Object ( [id] => 532 [rid] => 531 [created] => 1255384587 [nleft] => 4 [nright] => 7 [nlevel] => 2 ) [533] => stdClass Object ( [id] => 533 [rid] => 532 [created] => 1255384628 [nleft] => 5 [nright] => 6 [nlevel] => 3 ) )

感谢任何建议

谢谢

I have been looking at a N-level tree class.

Could anyone possibly advise on the best way I could display the results ie, the looping of the result set, should I use DIVs or ULs?

An example result set will look like so if you find the article too long to read

Array ( [531] => stdClass Object ( [id] => 531 [rid] => 0 [created] => 1255384549 [nleft] => 1 [nright] => 8 [nlevel] => 1 ) [534] => stdClass Object ( [id] => 534 [rid] => 531 [created] => 1255384549 [nleft] => 2 [nright] => 3 [nlevel] => 2 ) [532] => stdClass Object ( [id] => 532 [rid] => 531 [created] => 1255384587 [nleft] => 4 [nright] => 7 [nlevel] => 2 ) [533] => stdClass Object ( [id] => 533 [rid] => 532 [created] => 1255384628 [nleft] => 5 [nright] => 6 [nlevel] => 3 ) )

Any advice is apreciated

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2024-08-14 14:46:11

找到了我正在寻找的解决方案。

        function _generateTreeData(&$arr, $id, $level, &$n)
    {
        $arr[$id]->nlevel = $level;
        $arr[$id]->nleft = $n++;

        // loop over the node's children and process their data
        // before assigning the nright value
        foreach ($arr[$id]->children as $child_id) {
            $this->_generateTreeData($arr, $child_id, $level + 1, $n);
        }
        $arr[$id]->nright = $n++;
    }

现在进入格式化

Found the solution I was looking for.

        function _generateTreeData(&$arr, $id, $level, &$n)
    {
        $arr[$id]->nlevel = $level;
        $arr[$id]->nleft = $n++;

        // loop over the node's children and process their data
        // before assigning the nright value
        foreach ($arr[$id]->children as $child_id) {
            $this->_generateTreeData($arr, $child_id, $level + 1, $n);
        }
        $arr[$id]->nright = $n++;
    }

Now onto the formatting

幸福%小乖 2024-08-14 14:46:11

有点取决于您需要对输出做什么。我个人会使用未排序的列表或定义列表。

你想做什么?

Kinda depends on what you would need to do with the output. I would personally use unsorted lists or definition lists.

What are you trying to do?

清音悠歌 2024-08-14 14:46:11

将其输出为 UL 和 LI。如果输出太复杂,您可以在其顶部放置一个 JavaScript“树视图”组件 - 最好是可以自动将 UL 转换为树视图的组件。

Output it as ULs and LIs. If the output is too complex, you can put a JavaScript "Tree view" component on top of it - preferably one that can turn a UL into a tree view automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文