Lesson 30

Creating a Dynamic Menu

In this lesson, you learn how to create a menu that draws its information from the database rather than from hardcoded HTML. In the previous lesson, you created new articles through the website, but then you had to go into the index.php file to add the code to display the pages. By using a database, you are able to create menu links easily through the website as well. You create a table for the menu links, set up the maintenance for that table, and write the code to assemble and display the menu.

Setting up the Menu Table

The first step is to create the table that will contain the menu data. Here is some information you could have in a menu table, depending on the sophistication of the menuing system. The first two items are the minimum needed for a menu:

  • Menu Title: This is the text that is displayed in the menu.
  • Menu Link: This is the link for the menu item.
  • Order By: If you want to be able to decide in what order the links should appear, you need a field to sort the fields in a specific order.
  • Access Level: If you want to be able to show only authorized links, you need a field that gives the authorization. Depending on the complexity of the ACL, this could either be the access level itself or a key to another table that contains the access level information.
  • Menu: If you are creating more than one menu, you need to specify which menu the link belongs to. This should be a link to another table, which contains the information about the whole ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.