Table of Contents
Echo Menu Tree with Recursive Function
Problem Overview
Recursive Function Approach
PHP Function Implementation
Usage
Handling Empty Child Categories
Home Database Mysql Tutorial How to Create an Echo Menu Tree Using a Recursive Function in PHP?

How to Create an Echo Menu Tree Using a Recursive Function in PHP?

Oct 27, 2024 am 06:58 AM

 How to Create an Echo Menu Tree Using a Recursive Function in PHP?

Echo Menu Tree with Recursive Function

Problem Overview

Creating a recursive function to traverse a hierarchical menu structure stored in a database and output it in HTML can be challenging. Given a table with categories and their parent categories, the task is to generate a menu tree that visually represents the hierarchy.

Recursive Function Approach

To solve this problem, a recursive function is needed. The idea is to start with the root category, find its children, and recursively call the function on each child, building up the HTML output as we go.

PHP Function Implementation

Here is a possible implementation of the PHP function:

<code class="php">function recurse($categories, $parent = null, $level = 0)
{
    $ret = '&lt;ul&gt;';
    foreach($categories as $index =&gt; $category)
    {
        if($category['root'] == $parent)
        {
            $ret .= '&lt;li&gt;&lt;a href=&quot;#&quot;&gt;&lt;p class=&quot;Tier' . $level . '&quot;&gt;' . $category['name'] . '&lt;/p&gt;&lt;/a&gt;';
            $ret .= $this-&gt;recurse($categories, $category['id'], $level+1);
            $ret .= '&lt;/li&gt;';
        }
    }
    return $ret . '&lt;/ul&gt;';
}</code>
Copy after login
  1. It takes three parameters: $categories (an array of category data), $parent (the current parent category ID), and $level (which determines the level of indentation).
  2. It iterates through the categories and checks if their root matches the provided parent ID.
  3. If there is a match, it output the category name, wrapped in an li and a element with an appropriate Tier class for indentation.
  4. It then recursively calls itself with the next level of categories, setting the new parent ID and incrementing the level.

Usage

To use the function:

  1. Query the database to retrieve all categories.
  2. Call recurse() with the category data as the first parameter.
  3. Echo the returned HTML output.

Handling Empty Child Categories

The initial implementation may produce empty <ul> elements for categories with no children. To prevent this, you can modify the function like so:

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

This modification ensures that only categories with children have <ul> elements.

Alternatively, you can add a child count to each category and only include the <ul> if the child count is greater than zero.

The above is the detailed content of How to Create an Echo Menu Tree Using a Recursive Function in PHP?. 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

How do I configure SSL/TLS encryption for MySQL connections?

See all articles