Recursion example php,recursion in php mysql

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.


Published by

Jeetendra Singh

Jeetendra Singh is a UI Architect who has 10+ Years of Experience as Full Stack Developer. He is a Microsoft certified technology Specialist and AWS Certified Associate Architect. His Major Interests are in Mongo Db, Express Js, React Js, Node Js (MERN STACK). He build variety of applications using Mern, Mean, Node, JavaScript & jQuery

34 thoughts on “Steps to create dynamic multilevel menu using php and mysql”

  1. A wonderful example for diplaying a dynamic menu.
    Any chance we get a code (hint) on how to write the sortorder of each menu element back into the MySQL database, once the order of the menu is changed?

  2. 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

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

  4. 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

  5. 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





  6. 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.

  7. 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.

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

  9. 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 🙂

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

    1. 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

        1. 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

          1. Warning: mysqli_query() expects parameter 1

            Warning: mysqli_fetch_array() expects parameter 1

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

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

    1. 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.

Comments are closed.