Create a dynamic menu in PHP

The creation of a dynamic menu with a nested un-ordered list without using any JavaScript code is sometimes a problem because the script needs to “remember” the values, sub values and also the query string from previous clicked links. In this tutorial I will explain how to create a dynamic navigation menu using PHP and MySQ. While using standard HTML elements (un-ordered lists) it’s easy to add different CSS styles later. Follow this link for a demonstration example or download the PHP example files here.

First I need a database table holding the information for our navigation menu:

Create a dynamic menu in PHP from your MySQL data

Next I have to add some data for our PHP menu: menu items and sub-menu items.
Example: The menu item “Products” will have several sub-menu items and the record id is a “2”
All sub-menu items need the number “2” as value for the parent_id. The parent_id for each main menu item is a “0” (zero).

In the following code block I create a database result set and I add the value into a multi-dimensional arrays: $parent_menu and $sub_menu

The $parent_menu array is holding the label, the link (URL) and a count value that I need to decide if there is a sub-item or not. The $sub_menu array is holding the label and link values and the value of the parent_id.

I “loop” through the database result set to test each record’s parent_id is a zero (0) or not. If the value equal to “0” it’s a parent record and otherwise it becomes a sub-item record.

Put all the PHP code into a single function

For the creation of the whole navigation bar I a custom function, this way it’s more portable and I can access the code several times in different scripts without to write the code all over again.

The arguments of the function are both arrays, the name of the query string value that’s used to fold out the menu, the two ids used to identify the un-ordered lists with the CSS style and an extra name for some CSS pseudo class to give a main link element with sub-items a different look. A nested un-ordered list is a list with list items (“li” elements) and several other lists (“ul” elements). A main node can hold another lists or new “ul” elements.
To do this I start in my function with a “ul” element, to identify the element with CSS we add some id attribute here.

Inside the loop there is a test for the count value from parent items, if the count is not null the menu item query string value is added.

A PHP function which is used to manipulate the query string

The code block begins with a test for $_GET variable which is posted via the link from the parent menu item a new loop is opened to create a list with sub-items related to the parent item. Inside the statement the function rebuild_link() is called, a function which is used to manipulate the query string:

The arguments for this function are the link from the array, the variable name and value from the parent menu item (to keep the list open). Inside the function the query string will be exploded and is getting rebuilt without becoming in trouble with existing parts or new variables. At the end, the function will return a modified link with all parts of the query string.

Example PHP dynamic menu usage

How to use this dynamic menu PHP function? It’s easy just echo the function name  with all the function attributes:

Don’t forget to try the PHP menu demo for a better understanding.

2 thoughts on “Create a dynamic menu in PHP

  1. Good script. However, there seems to be an error on lines 4 and 5 :

    $db = new mysqli(‘localhost’, ‘db_user’, ‘db_password’, ‘db_name’)) {
    $items = $db->(“SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC”);

    Line 4 : Should remove the ending ){ and replace /close it with ;
    Line 5 : Forgot to add the query method $items = $db->query(“SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC”);

    • Thanks Dan, I guess I did some wrong copy/paste from my demo page 🙂
      The code is fixed.

Comment Rules

Don’t post your code here, post your code block or snippet to pastebin and include the pastebin URL in your comment.

I delete all comments with non related links inside the comment text. Don't use keywords for the field of your real name (most people like to use your name for their answer). Keep your comment related to the topic, if your question is off-topic, please use the contact form instead.

Leave a Reply

Your email address will not be published. Required fields are marked *