We can create a simple dynamic navigation menu with PHP MySQL in just a few steps: Create a database table to store the menu items. Create a PHP script to fetch the menu items from the database. Finally, draw the menu in HTML. But just how is this done?
How do I include a PHP file in a navigation menu?
pinch of PHP. All it takes is some basic PHP to include the content of navigation.php. PHP’s include () function offers a handy way to call an external file from the server. Simply replace the navigational menu with this line of code, being sure to adjust for the location of the file on your server.
Why should I use dynamic web pages?
The occasions to make mistakes, or to forget to modify some of the pages, could easily make the website quite hard to manage. Such a situation can be easily fixed by turning to dynamic web pages generated with PHP. By looking at the code of the three pages, it is immediately evident that they do have a lot in common.
What is the purpose of the navigational menu?
The navigational menu indicates which page the user is on by displaying the Page Two link in a different color and background. The down side? As a developer, you must remember to manually remove id=”currentpage” from one link to the next as you develop each page. Not so if you use PHP.
How do I get the menu items in the database?
Connect to the database. Remember to change the settings to your own. Use a while loop to automatically drill down to get the menu items. The menu items are in the arrangement of $items [PARENT ID] [ITEM ID] = MENU ITEM. Close the database connection.
How to create a mySQL table with recursion in MySQL?
Create a mysql Table in your database by executing following sql statement Step 2. Connect with database Step 3. Implement the recursion logic $sqlquery = ” SELECT * FROM menu where status=’1′ and parent_id='” .$parent_id .
What is the Parent_ID for each sub_menu item?
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