Home > Database > Mysql Tutorial > body text

How to Build a Limitless Nested Menu System with PHP and MySQL: A Non-Recursive Approach?

Linda Hamilton
Release: 2024-10-29 18:41:12
Original
426 people have browsed it

How to Build a Limitless Nested Menu System with PHP and MySQL: A Non-Recursive Approach?

How to Display Unlimited Nested Menus in PHP and MySQL

Problem Description

In a nested menu system, each menu item can have child menu items, which can in turn have their own child items. The goal is to fetch and display these nested menu levels from a database effectively.

Code Implementation

To achieve this, we can employ a non-recursive approach that involves:

  • Database Query: We query the menu_item table, ordering the results by their parent and position.
  • Multi-dimensional Array: We organize the retrieved menu items into a multi-dimensional array, where each element represents a parent-child relationship.
  • Tree Traversal: We traverse this array using a loop.
  • HTML Generation: We dynamically generate the HTML structure for the nested menus.

PHP Function

Here's a PHP function that demonstrates the above approach:

<code class="php">function generateMenu($items) {
    $html = '';
    $parent = 0;
    $parentStack = array();

    $children = array();
    foreach ($items as $item) {
        $children[$item['parent_id']][] = $item;
    }

    while (($option = each($children[$parent])) || ($parent > 0)) {
        if (!empty($option)) {
            // 1) Item with children
            if (!empty($children[$option['value']['id']])) {
                $html .= '<li>' . $option['value']['title'] . '</li>';
                $html .= '<ul>';
                array_push($parentStack, $parent);
                $parent = $option['value']['id'];
            }
            // 2) Item without children
            else {
                $html .= '<li>' . $option['value']['title'] . '</li>';
            }
        }
        // 3) Current parent has no more children
        else {
            $html .= '</ul>';
            $parent = array_pop($parentStack);
        }
    }

    return $html;
}</code>
Copy after login

Usage

To use the function, first retrieve the menu items from the database and pass them as an array to the generateMenu() function. The resulting HTML will be a series of nested unordered lists representing the hierarchical menu structure.

Benefits of Non-Recursive Approach

This non-recursive approach eliminates the risk of infinite loops that can occur with recursion, making it a more stable and efficient solution for generating nested menus.

The above is the detailed content of How to Build a Limitless Nested Menu System with PHP and MySQL: A Non-Recursive Approach?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!