active link with php navbar

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 7
Joined: Sat Aug 25, 2012 10:23 pm

Wed Mar 27, 2013 2:06 pm

I would like to include my nav.php nav bar to all my pages. but I need the links to change based on active page.

css is done and works fine but I am trying to convert this navbar to php to make it easier to work with in the future.

this is my navbar in the nav.php file what do i need to add to it to make it change active links per page it is on

$index = "topnav";
$contact = "topnav";
$services = "topnav";
$employees = "topnav";

//This line gets the file name without the suffix
$index = 'active';
$contact = 'active';
}elseif($menuLinkid == "services"){
$services = 'active';
}elseif($menuLinkid == "employees"){
$employees = 'active';

<li><a class="<?php echo $index; ?>"href="index.php">Home</a></li>
<li><a class="<?php echo $contact; ?>"href="contact.php">Contact Us</a></li>
<li><a class="<?php echo $services; ?>"href="services.php">Services</a></li>
<li><a class="<?php echo $employees; ?>"href="employees.php">Employees</a></li>


User avatar
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI

Thu Mar 28, 2013 8:42 am

Don't know exactly what you're trying to do, but maybe something like the following?

Code: Select all

$output = "<ul>";

$output .= "<li><a class=\"" . $index.php . "\" href=\"index.php\">Home</a></li>";
$output .= "<li><a class=\"" . $about.php ."\" href=\"about.php\">About Us</a></li>";
....additional links....

$output .= "</ul>";

echo $output;

Post Reply