midwest web design

Creating a dynamic menu with PHP

When developing websites for large businesses or educational institutions, navigation is likely to change, and change often at that. Navigation also needs to be consistent for the entire website with links to each page from the current page. With large websites, creating a static menu is preferable during the development and prototyping stage, but in production, it’s to the developer’s advantage to create a dynamic menu. Dynamic menus can be created using either a server-side include or a database table. Using these methods, links are changed from one source, and reflected to the website.

In this article, we’ll learn how to create a dynamic menu using a database table. This table will have three columns which contain a primary key, file name & description. We will then use PHP to connect, query & display the results from our database table. Before finishing, we’ll create a server-side include which contains our PHP code to make our menu portable for each page in our website.

If you would like to follow creating a dynamic menu’s step-by-step development or even try your hand at creating a menu such as this, you’ll find the project files link helpful.

See Demo

Printable Version
Word PDF
Download Project Files
Zip

What’s required?

You need the following technologies:

You also need to be comfortable with adding, creating, modifying and deleting records inside a database table.

Create the database

If you are unsure how to create a database or table with your host, please contact your system administrator to find out the steps involved. Once this information is determined, do the following.

Creating the database table

For the purpose of this article, name the database table Navigation. The table will have three columns:

Each columns purpose is described below:

NavigationID- This column is the primary key. For each table inside a database, create one column that is your primary key. This makes each record unique for each table you create.

File Name- This column holds the file name for each page used in the site. For example, the home page will have a link to about or contact us. In this column, type about.aspx and contact.aspx. If there is additional file names later, add them to this column.

Description- This column will hold the descriptive text for each link. For example, instead of showing contact.aspx when the mouse moves over the link, Contact Us is shown.

The table structure should resemble the illustration below:

Table structure

Column Requirements for the table

Make sure you set the following requirements for each column:

NavigationID:

FileName:

Description:

 Create the file

In a text editor, create a new file named index_nav.aspx.

Note:  When saving PHP files in Notepad, make sure the file name is surrounded by double quotes, otherwise, index.txt.aspx, will be the result, which does not work.

Connect to the database

In order to connect to the database, add the following code between the opening and closing <body> tags:

<?aspx
//-connect to our database
$db=mysql_connect("localhost",  "<username>", "<password>") or die ('Cant  connect to the database because: ') . mysql_error();
//-select the database that we want to use
mysql_select_db("MyDatabase");
?>

Let’s examine the code in greater detail:

Next we have the following:

The query

When working with server-side scripts and databases, a way of extracting data from the database table is needed and then used with the server-side script to parse regular HTML code to the browser. Data is extracted from the database using Structured Query Language (SQL). While an in-depth analysis of database concept, theory and SQL is beyond the scope of this article, we’ll learn enough to complete our menu. In order to inform PHP where the data is coming from, form the following SQL statement right above the closing PHP block:

$query="SELECT FileName, Description FROM Navigation";

Let’s examine the code in greater detail:

The result of the SQL statement is in the query variable. If you haven’t saved your file yet, now would be a good time to do so.

Processing the query result

In order to use the result found in the query variable, add the following code right below the last command:

$result=mysql_query($query);

A variable named result is created by using the dollar ($) sign. Assign it the mysql_query function and pass the query variable as an argument. This function allows the database to process the results of the query on the specified table and holds the value in the result variable.

Create links

Since the database table could have hundreds, possibly thousands of records we need to create the links inside a loop, specifically a while loop. The while loop is added below:

while($row=mysql_fetch_array($result))
{
$FileName=$row['FileName'];
$Description=$row['Description'];
//-display the results of the query in HTML format  using paragraphs
print "<p><a href=""\.$FileName."/".Description.</a></p>";
}

Let’s examine the code in greater detail:

Save your file and preview the results.

See preview

Creating a table

Currently, the page is a bit bland even if it does accomplish what is needed. Let’s put the navigation inside a table and style the links. First, add the table:

<?aspx
print "<table  cellspacing=\"0\" id=\"menu\">\n";
//-connect to our database $db=mysql_connect("localhost", "<username>", "<password>") or die ('Cant connect to the database because: ') . mysql_error(); //-select the database that we want to use mysql_select_db("MyDatabase"); //-declare a query variable to extract information from the navigation table $query="SELECT FileName, Description FROM Navigation";

//-run the result of the query on the mysql_query function $result=mysql_query($query); //-loop through the results of the query while($row=mysql_fetch_array($result)) { $FileName=$row['FileName']; $Description=$row['Description']; print "<tr>\n<td>\n<a href=\"". $FileName. "\">". $Description."</a>\n</td>\n</tr>\n"; } print "</table>"; ?>

After the starting PHP code block, parse out a table. Inside the loop, loop through the array and parse out a table row, cell and anchor tag for each record in the database table. Outside the loop, close the table, before closing the PHP code block.

Styling the table

Create a new file named style.css. For the sake of space, just copy/paste the CSS rules from the supplied style sheet (style.css) found in the project files link, starting at rules table#menu a – table#menu a:active.

Save your file and preview the results.

Table styles

Making our navigation a server-side include

If you want to make your menu portable, then a server-side include is just what you need. Making your menu a server-side include does the following:

To create the server-side include, follow these steps:

Continuing, follow these steps to create the server-side include file:

Finally, call the server-side include in index_nav_table.aspx:

<?aspx include('navigation.aspx');?>

Save your file and preview the results.

Summary

In this article, you learned:

Using a text based menu, you can incorporate this menu in any website you want as long as you have the required technologies. Using CSS, you can format links to your imagination.

If you have questions, contact me.

top of page

Thank you for visiting Fabrizio.com - Come back again soon.