Simple website navigation with PHP

A good website is easy to navigate from one page to the next and an easy to navigate
website will make the following clear to the user:

  • Which page he is currently viewing, i.e. where he currently “is” in your
    website
  • What pages he can view

Furthermore, you want to be able to save time and effort in
coding your website while also ensuring that your site can be easily modified and updated.This is
an ideal situation in which to use PHP server side scripting to dynamically generate the links.

In this post I present code that will generate links grouped under different sub-headings. It will apply a special style to the navigation bar entry for the page that the user is currently viewing.

Links array

Titles, filenames about all the webpages in the site should be placed in an associative array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$links=array("This site"=>array("index.php"=>"Home",
				"guestbook.php"=>"Feedback"
				),
			"Freeware"=>array("sysutils.php"=>"System Utilities",
				       "netsoft.php"=>"Internet Software",
				        "games.php"=>"Games",
				        "ppt.php"=>"Powerpoint Templates",
				       "webdev.php"=>"Website Development",
				       "imgediting.php"=>"Image Editing",
				       "miscutils.php"=>"Assorted Utilities"
            			                       )
        	);
?>

In the above array, “Freeware” and “This site” are the categories of webpages. On-screen these will show up as headings. Each of these contains a sub-array that lists the filename and title of the webpages in that category. When adding or removing pages from your site, you only need to update the above array.

Generating links

The following code iterates through the links array to generate the HTML hyperlinks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
foreach($links as $category=>$pages)
{
    //display navigation heading
    ?>
    <h1>
    <?echo $category?>
    </h1>
    <?                        
     foreach($pages as $url=>$title){
     //check to see whether it is the current page
       if (basename($curpage)==$url )
       {
          //apply speacial style to current page        
          ?>
           <span class="currentpage">
             <?echo $title;?>
           </span><?
        }
         else
         {
           //otherwise insert link
            ?><a href="<?echo $url ?>"><?echo $title;?></a><?
          }
       }
}        
 
?>

The outer foreach loop iterates through the $links array. At each iteration it displays the appropriate links category heading. The inner loop iterates through all the pages in the current category sub-array. At each iteration it compares the current file name (as stored in the array) with the filename stored in the $curpage variable. This variable contains the full path to the page that visitor is currently viewing. A hyperlink is not inserted for the current page to avoid confusing the visitor. Links to the other pages in the category are automatically included.

Including the code

All the code presented so far can be placed in an include file. For ex: nav.php.
To display the links you must tell PHP to include this file in a webpage inplace of your usual navigation bar/panel. To do that you will have to add the following code to every webpage:

1
2
3
4
<?
$curpage= __FILE__;
include "pathtoincludefile/nav.php";
?>

This also sets $curpage to equal the PHP constant variable __FILE__ which contains the full path to the webpage. For ex: /foo/foo.php. Every webpage that has the above code must have a filename with the extension “.php”. A .php extension tells the webserver that the file should be sent to PHP to be parsed and executed before
it is sent to a user’s browser.

Setting the title

Since the title changes from webpage to webpage it can also be set using PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
<title>Site Name -  <?
 
foreach($links as $category=>$pages)
{
 
//if page is a part of current category display title
if(array_key_exists(basename($curpage),$pages))
  echo " - ". $category." - ".$pages[basename($curpage)];
 
}
?>
 
</title>

This code can be placed in a separate include file or as part of the navigation include file depending on your web page style.

Using this technique you can update all the navigation panel links in all your webpages by editing one file.

2 thoughts on “Simple website navigation with PHP

    • This was great! It was exactly what I needed: a simple, no nonsense “this is navivgation in php”. You didn’t require tables – just a simple array.

      Thank you!

      Kenneth Udut
      naplesplus.us and naplesnerds.com
      [always striving to find the perfect ui]

Leave a Reply

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