Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

php - Echo menu tree with recursive function

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

Situation:

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

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>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about:

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>';
}

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;

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

In which your function would be:

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>';
}

Hope that helps?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...