Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

active link with php navbar

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

active link with php navbar

Postby nichodiaz » 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


<?php
$index = "topnav";
$contact = "topnav";
$services = "topnav";
$employees = "topnav";

//This line gets the file name without the suffix
$menuLinkid=basename($_SERVER['PHP_SELF'],".php");
if($menuLinkid=="index"){
$index = 'active';
}elseif($menuLinkid=="contact"){
$contact = 'active';
}elseif($menuLinkid == "services"){
$services = 'active';
}elseif($menuLinkid == "employees"){
$employees = 'active';
}
?>
<ul>

<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>


</ul>
nichodiaz
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Aug 25, 2012 10:23 pm

Re: active link with php navbar

Postby Strider64 » 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;
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron