Home > Database > Mysql Tutorial > How to Build Unlimited Levels of Menus with PHP and MySQL: A Step-by-Step Guide?

How to Build Unlimited Levels of Menus with PHP and MySQL: A Step-by-Step Guide?

Barbara Streisand
Release: 2024-10-30 18:57:03
Original
413 people have browsed it

How to Build Unlimited Levels of Menus with PHP and MySQL: A Step-by-Step Guide?

Building Unlimited Levels of Menus with PHP and MySQL

In website development, menu navigation plays a crucial role in user experience. However, building menus that support unlimited levels of submenus can be challenging. Let's delve into a comprehensive approach to achieve this functionality using PHP and MySQL.

Database Structure and Menu System

The database structure you mentioned is a common approach to represent a hierarchical menu system. Each menu item has a parent field to establish the parent-child relationship. Parent ID 0 indicates top-level menu items.

Retrieving Top-Level Menus

Your existing PHP code for retrieving top-level menus is correct:

<code class="php">$list = $obj->childmenu($parentid);
foreach($list as $menu) {
    echo '<li><a href="#">'.$name.'</a></li>';
}</code>
Copy after login

Identifying Submenu Existence and Display

Your query aims to identify if a menu item has child menus and display them within its list item. To achieve this, you need to modify your SQL query and PHP code.

Optimized SQL Query

Using a single SQL query, we can retrieve all menu items ordered by their parent ID and position:

<code class="sql">SELECT id, parent_id, title, link, position FROM menu_item ORDER BY parent_id, position;</code>
Copy after login

Refined PHP Code

The revised PHP code incorporates a stack to track the current menu level and ensures efficient handling of nested menus:

<code class="php">$items = $query_results;  // Assuming $query_results contains the result of the SQL query

$html = '';
$parent = 0;
$parent_stack = array();

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

while ( ( $option = each( $children[$parent] ) ) || ( $parent > 0 ) ) {
    if ( !empty( $option ) ) {
        if ( !empty( $children[$option['value']['id']] ) ) {
            $html .= '<li>' . $option['value']['title'] . '<ul>';
            array_push( $parent_stack, $parent );
            $parent = $option['value']['id'];
        } else {
            $html .= '<li>' . $option['value']['title'] . '</li>';
        }
    } else {
        $html .= '</ul>';
        $parent = array_pop( $parent_stack );
    }
}

echo $html;</code>
Copy after login

This updated code will effectively process the menu structure, create HTML markup for nested menus, and display them within their respective list items.

The above is the detailed content of How to Build Unlimited Levels of Menus with PHP and MySQL: A Step-by-Step Guide?. 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