Home > Database > Mysql Tutorial > How can recursion be used to generate a nested menu tree from a database with parent categories?

How can recursion be used to generate a nested menu tree from a database with parent categories?

Susan Sarandon
Release: 2024-10-30 01:41:28
Original
216 people have browsed it

How can recursion be used to generate a nested menu tree from a database with parent categories?

Recursion for Menu Tree Generation

In your situation, you have a database structure where categories have a 'root' field indicating their parent category. The HTML output you desire involves nested lists representing the category hierarchy. To achieve this, a recursive PHP function can be employed.

Here's an example 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

This function assumes that root categories have a 'root' field with a null value. It iterates through the categories, identifies child categories based on their 'root' field, and constructs the HTML using nested lists.

To use this function:

  1. Query your database for a multi-dimensional array of categories.
  2. Call the recurse function with the array of categories and null as the parent parameter.
  3. Assign the returned value to a variable (e.g., $Tree).
  4. Echo the $Tree variable to display the nested menu tree.

Optionally, you can modify the function to avoid empty lists by checking if the category has child categories before creating the list:

<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>';
            $sub = $this->recurse($categories, $category['id'], $level + 1);
            if ($sub != '<ul></ul>') {
                $ret .= $sub;
            }
            $ret .= '</li>';
        }
    }
    return $ret . '</ul>';
}</code>
Copy after login

This modified function will only create lists if the category has at least one child.

The above is the detailed content of How can recursion be used to generate a nested menu tree from a database with parent categories?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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