具有递归功能的回显菜单树 [英] Echo menu tree with recursive function

查看:26
本文介绍了具有递归功能的回显菜单树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我想不出针对我的具体情况制作递归函数的方法.

Problem: I can't think of way make a recursion function to my specific situation.

情况:

Mysql DB
<代码>id |根|姓名|
root 向女巫类别显示的地方,这是子类别.

Mysql DB
id | root | name |
Where root shows to witch category this is subcategory.

HTML 应该是什么样子:

How should HTML look:

    <li><a href="#"><p class="Tier0">Datori</p></a>
                    <ul style="display: block">
                         <li><a href="#"><p class="Tier1">Cookies</p></a></li>
                         <li><a href="#"><p class="Tier1">Events</p></a></li>
                         <li><a href="#"><p class="Tier1">Forms</p></a></li>
                         <li><a href="#"><p class="Tier1">Games</p></a></li>
                         <li><a href="#"><p class="Tier1">Images</p></a>
                            <ul>
                                <li><a href="#"><p class="Tier2">CSS</p></a></li>
                                <li><a href="#"><p class="Tier2">JavaScript</p></a></li>
                                <li><a href="#"><p class="Tier2">JQuery</p></a></li>
                            </ul>
                         </li>
                         <li><a href="#"><p class="Tier1">Navigations</p></a>
                            <ul>
                                <li><a href="#"><p class="Tier2">CSS</p></a></li>
                                <li><a href="#"><p class="Tier2">JavaScript</p></a></li>
                                <li><a href="#"><p class="Tier2">JQuery</p></a></li>
                            </ul>
                        </li>
                         <li><a href="#"><p class="Tier1">Tabs</p></a></li>
                    </ul>
                </li>
                <li><a href="#"><p class="Tier0">Washing Machines</p></a>

我需要什么样的 PHP 函数才能将其全部打印出来?

What kind of PHP function I would need to print it all out?

推荐答案

怎么样:

function recurse($categories, $parent = null, $level = 0)
{
    $ret = '<ul>';
    foreach($categories as $index => $category)
    {
        if($category['root'] == $parent)
        {
            $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
            $ret .= $this->recurse($categories, $category['id'], $level+1);
            $ret .= '</li>';
        }
    }
    return $ret . '</ul>';
}

此函数要求您首先查询数据库以获取可用类别的整个列表,并假设您的根类别的值为 null,但该函数可以更改为接受 -1 或 0,具体取决于您当前模式的工作方式.

This function requires that you first query your database for the entire list of available categories and assumes that your root categories have a value of null, but the function can be changed to accept -1 or 0 depending on how your current schema works.

$categories = { get from database into an multi-dimensional array };
$Tree = $this->recurse($categories);
echo $Tree;

当父级不存在子级时,您可以考虑执行以下操作以防止出现任何空的 UL:

You may consider doing the following to prevent any empty UL's appearing when no children exist for the parent:

function recurse($categories, $parent = null, $level = 0)
{
    $ret = '<ul>';
    foreach($categories as $index => $category)
    {
        if($category['root'] == $parent)
        {
            $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
            $sub = $this->recurse($categories, $category['id'], $level+1);
            if($sub != '<ul></ul>')
                $ret .= $sub;
            $ret .= '</li>';
        }
    }
    return $ret . '</ul>';
}

但是,最好的解决方案是选择您的数据以包含一列,其中包含每个类别有多少个子类别.

However, the best solution, would be to select your data to include a column containing how many child categories each category has.

select Category.*, (select count(distinct c1.id) from Category as c1 where c1.root = Category.id) as ChildCount from Category

您的函数将在其中:

function recurse($categories, $parent = null, $level = 0)
{
    $ret = '<ul>';
    foreach($categories as $index => $category)
    {
        if($category['root'] == $parent)
        {
            $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
            if($category['ChildCount'] > 0)
                $ret .= $this->recurse($categories, $category['id'], $level+1);
            $ret .= '</li>';
        }
    }
    return $ret . '</ul>';
}

希望有帮助吗?

这篇关于具有递归功能的回显菜单树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆