Steps to create dynamic multilevel menu using php and mysql

Hi Geeks,
Today i am sharing you ” Steps to create dynamic multilevel menu using php and mysql” which can be called “example of recusion in php & mysql”.
What Exactly recursion is ?
Recursion is a technique or Algorithm by which we can go and return the collection of sequenced data with n depth. It is a condition based child search and return the output to the previous stepped varriable’s scope.
we can solve many of the programming problems using recursion.
In following Example we will sort out a problem where we need to show nested n number of depth relation saved in mysql menu table with having proper opening and closng the <ul> and <li> tags in html.
Following are the steps :

Step 1. Create a mysql Table in your database by executing following sql statement

Step 2.  Connect with database

Step 3. Implement the recursion logic

Step 4: Ininitialize  start the Recursion in Parent Content Holder

Step 5: Close the Mysql connection

Step 6. <ul> <li> depth css implementation in style tag in head section

Conclusion : Complete Code in one File.


  • Rado

    hi, on localhost function works perfectly but after web upload i am getting fatal error of memory size. is there any other solution to fix it ? suppose this is caused by infinite loop in call recursively. thank you

  • Stania

    To deal with the issue just add $menu = str_replace(”,”,$menu); after the $menu .= “”;

  • Patel Gopi

    can you give me this code using select box on email id

  • gurram santhosh

    Hi,Can you please help me in the following scenario
    In your program how to insert data dynamically based on user view and also is there any possible to add data dynamically by clicking links.please try to give solution as soon as possible .its very urgent to me

  • Abdul Mumin Yahaya

    I will first of all appreciate you for this script.

    I discovered that where there is no child node, empty UL tags () will be left behind. Please how can we remove that base on this code


    Web development


    Elastic Ip

    Load balacing

    Cluster Indexes

    Rds Db setup


    Framework Development



    Ecommerce Development



    Cms Development




    News & Media




    WordPress Development



    Custom Post Types





  • lakshmi prasu

    Thanks a lot its very helpful to me. I am searching about this script nearly one day.I am using sub while loop . Because I want sidebar list with subitems.Anyway I will modify it.Once again Thank u.

  • T!!

    please do you save the code in a .html file or a .php file?

    • Fakrudin abu bakar

      Complete Code: .php

  • Aravinth Sakthivel

    i need tp parse json nested array in php code anybody help me

  • amitabh kumar

    SEO frendly page link click on menu link so please help me

  • amitabh kumar

    sir your code is very use full but one issue how can open click on menu link display contant diffrence so please help mi

  • bikers travellingsouls

    how to make use this code inside bootstrap menu

    • You can follow my way which I have uploaded in pastebin –
      Hope it will help you. If you find something better than my one, please share with me.

  • zeeshan chaudhary

    why u use parent_id
    and from which tabel u get parent_id

    • Fakrudin abu bakar

      same table; menu

  • Chukwudi onwumere

    Thanks for the speedy response to my mail. Your suggestion fixed the bug. Thanks alot. I have been surfing the net for hours yet couldn’t get a solution.

  • Manoj Kumar Vattakkat

    can you please describe how to add 3rd and more level menus in the database

  • Billa Mounika

    Warning: mysqli_query() expects parameter 1 to be mysqli,
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

    Hi, even i am getting the same error. what does it mean> pls hlp :)

  • Anil Reddy

    Thanks a lot. It is the best solution I have got after searching for 2 hours.

    • w3school

      Welcome Anil . Its my pleasure , if you need any problem you can discuss with me directly by email on

  • envyyy

    Ok i’ve set it up like said. What do i have to name my PHP files for the navigation to find them?

    • w3school

      I have provided a function in step number 3 , so either you can put that function in your existing functions.php file or if functions.php file not exists then you can create functions.Php file and put that function in it and include by using include or include_once php methods in your running file like index.php and call that function inside tag like step 4,
      If any problem in it then let me know your email I’d I will send a demo to you but by tomorrow as it’s too late in India ,hope you can understand. Cheers

      • envyyy
        I also have another problem, but ill let you know via email by then, thank you!

        • w3school

          No issues you can also post your another problem in public comments and I will send you complete working demo by tomorrow on your email id

          • envyyy

            Warning: mysqli_query() expects parameter 1

            Warning: mysqli_fetch_array() expects parameter 1

            Warning: mysqli_close() expects parameter 1
            What am i doing wrong?

            • w3school

              Hi Envyy i have sent you a mail having complete demo , i that zip file there is a readme.txt file please read that

  • Exal

    This is two level menu. But if I have 3,4,5 levels?

    • w3school

      No Exal this is 4 level menu and this code works with 3,4,5 and nth level of relation ,you can add n number of iterations in menu table please check with adding more entries and see the screenshot , this is 4th level working very fine.

  • Dhruv

    Thats great.
    I have one more query, may i ask ?

    • w3school

      yes please what is your query?

    • amitabh kumar

      how to display contant on click menu link sir seo frendly