Hello friends, today I will show you how to implement dynamic menu generator using php. Every website has menu, with out menu a website is incomplete. So I build a script for you to dynamically generate menu using php, mysql, css, js. It is a very good script to implement menu in your website. The main features of this application is that it is fully user customizable, responsive, database driven, lightweight and ul li structure.
The concept of dynamic menu generator script is that all the menu item will be store in a database table with menu name, link, parent menu id. I create a buildMenu recursive function which call himself every time when the menu has sub menu. I use a js and css file for make the menu in drop down style. So lets build php dynamic menu generator step by step. I assume that you have a static menu with ul li structure. So lets build the application.

Dynamic menu generator php script

Step 1: Create the database table for storing all menu item.
title text NOT NULL,
link text CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
parentid bigint(20) NOT NULL DEFAULT ‘0’,

INSERT INTO main_menu (id, title, link, parentid) VALUES
(1, ‘Home’, ‘http://abhijitpal.in’, 0),
(2, ‘Products’, ‘http://abhijitpal.in’, 0),
(3, ‘About’, ‘http://abhijitpal.in’, 0),
(4, ‘Contact’, ‘http://abhijitpal.in’, 0),
(5, ‘Product 1’, ‘http://abhijitpal.in’, 2),
(6, ‘Product 2’, ‘http://abhijitpal.in’, 2),
(7, ‘Sub Product1’, ‘http://abhijitpal.in’, 5),
(8, ‘Sub Product2’, ‘http://abhijitpal.in’, 5),
(9, ‘Sub Product3’, ‘http://abhijitpal.in’, 5),
(10, ‘Sub Product4’, ‘http://abhijitpal.in’, 6),
(11, ‘Sub Product5’, ‘http://abhijitpal.in’, 6),
(12, ‘Sub Sub Product1’, ‘http://abhijitpal.in’, 9),
(13, ‘Sub Sub Product2’, ‘http://abhijitpal.in’, 9)

Step 2: In this step I convert static menu to dynamic menu. Here I use a single sql query to fetch all records from menu table and create a multidimensional menu array to hold a list of menu and parent menu. Then I use this array in buildMenu function to generate multi-level menu. buildMenu function recursively call and take parentid, and menu array as a function parameter. At last I call buildMenu function to generate full menu.

Finally it produce me this output.
dynamic menu generator


Spread the love
Php multi-level dynamic menu generator script
  • Bill Hogsett

    Very nice.

    Is there a way to exclude the current page from the menu?

  • http://electrocompsystem.com/ Surendra Narayan Roy

    Thanks for your invention. One thing I want to ask you.
    How will I change the default menu ( or selected top level menu) for different pages. That I mean the moving top line on the top of the menu bar, I want the set the line on another place when i go in another page form the menu.

    Please help me.


  • hack-club

    thank my friend

  • Sandy Sailer

    This is such an excellent tutorial! One question … what if I wanted to use SEO-friendly URLs instead? How would I go about that?

    Thank you so much for these instructions!

  • sudarshan94

    Call to a member function fetch_assoc() on a non-object
    in laravel what is the problem im in laravel 5.2

  • Makoto

    how to convert to java or javascript?

  • Preeti

    its an excellent tutorial and I have implemented also successfully.
    But pls help me to convert this horizontal menu to vertical menu. I have to implement my menu at left side of my page vertically…