0

Currently for my projects I create the navigation for each page manually and it looks something similar to:

<nav> <ul id="mainMenu"><!--Main Menu--> <li><a class="active" href="index.php">Home</a></li> <li><a href="contact.php">Contact</a></li> </ul> </nav> 

This works fine, however for projects that has many many pages it is not a really good practice or even efficient to do it manually. So I was wondering if there is anyone who can direct me to the right path and advice me on how to make my navigation dynamic? I know about PHP include and the .inc files - they are good. BUT I want to add class .active to the <a> of the page that is currently open. How can I do that?

BTW: I don't know if this s the right place to post this sort of questions here, but the moderator told me to post it here.

1 Answer 1

1

Use include to add a central php file, that contains a function which can take the current page as a parameter:

nav.inc:

function renderNavigation($current_page) { //render you navigation } 

main.php:

require_once("nav.inc"); renderNavigation("Subpage 1") 
Sign up to request clarification or add additional context in comments.

3 Comments

So basically add the navigation in a file like nav.inc for example and include the php in each page. This work I know. The problem I am having is with adding the class .active to the button of the current open page just to tell the user that you are viewing this page... I don't really know about URL Parameter, can you please explain it a bit more clearly? thanks.
My example is the content of the nav.inc file. Let the include not render directly, but instead let it provide a function you can call from your main page that renders a navigation (using whatever marker you pass to figure out the current page).
I am really sorry! but can't seem to be able to understand this technique is there a name for it? so I can research it more? Please advice.