php-how to use includes for navigation

by webgarden ~ February 4th, 2008. Filed under: php.

Learning php has not been an easy task! But I have been plugging away at it, and now I have a few tricks under my belt to use php to make web design easier.

Today I am going to give an example of the power of “includes”. In my opinion using includes for your navigation is probably the most useful of all the includes!

So, how does it work?

When you use an include for your navigation, instead of having to make changes to each page where the navigation is, you open your include file, make the changes and upload the ONE file. How do you do it?

Well, it’s very easy.

  1. Create a blank php page. (there should be nothing on the page – no doc type declaration – NOTHING!)
  2. Rename the page “navInclude.php” and save it inside a folder called “includes”
  3. Create an unordered list with your navigation links. (IE. home.php, aboutUs.php, Contact.php etc..)      
  4. Make sure your navigation list in “navInclude.php” is linked to the correct pages
  5. Go to your index.php page. Find the div where your navigation is. <div=”nav”></div>
  6. Between the two navigation tags insert this code:
    <?php include(’includes/navInclude.php’); ?>
    (You must delete your current navigation links before inserting the php code)

    So it looks like this: <div=”nav“><?php include(‘includes/navInc.php’); ?></div>

  7. Repeat this for each page in your site
  8. Upload your pages, and the folder “includes” and voila! Your navigation is working!
  9. Now, if you want to change the “About” page to “Our Staff” all you have to do is change the navInclude.php, upload it, and the changes will be made to the entire site! How great is that?

Later this week, I will post another simple php lesson. If you have any questions, please post a comment and I will do my best to reply!

1 Response to php-how to use includes for navigation

  1. Dave Blencowe

    Sometimes you find that code including with the include(); directive is executed twice in a page and starts producing session errors and the like.

    One way around this is to use the require_once(); directive. http://www.php.net/require_once

Leave a Reply