Home > Database > Mysql Tutorial > How can I create a recursive PHP function to generate a hierarchical menu tree from a MySQL database table?

How can I create a recursive PHP function to generate a hierarchical menu tree from a MySQL database table?

Mary-Kate Olsen
Release: 2024-10-29 00:28:02
Original
986 people have browsed it

How can I create a recursive PHP function to generate a hierarchical menu tree from a MySQL database table?

Echoing Menu Tree with Recursive Function

To generate a hierarchical menu tree from a database table using a recursive function, consider the following approach:

Problem:

You wish to create a recursive function that extracts a hierarchical menu tree from a MySQL database.

Situation:

The table contains columns:

  • id
  • root (ID of the parent category, or null for root categories)
  • name

Expected HTML Output:

<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></li>
Copy after login

Proposed PHP Function:

<code class="php">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>';
}</code>
Copy after login

Usage:

  1. Fetch the category list into a multi-dimensional array.
  2. Call the recurse function with the category array and the output will be the desired HTML menu tree.

Additional Considerations:

  • Prevent empty UL tags by checking child count before appending them.
  • Consider using a database column to store child count for efficiency.

The above is the detailed content of How can I create a recursive PHP function to generate a hierarchical menu tree from a MySQL database table?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template