I have a number of pages within directories and i have added navigation via an include file. I need the ability to add an active class to the nav item depending on what page you are on.
<?php
if(isset($SERVER["PHPSELF"])) {
if ($SERVER["PHPSELF"]=="index.php") $status="class='active'"; else $status="";
echo '<li><a ".$status." href="index.php">Home</a></li>';
if ($SERVER["PHPSELF"]=="gallery.php") $status="class='active'"; else $status="";
echo '<li><a ".$status." href="gallery.php">Gallery</a></li>';
if ($SERVER["PHPSELF"]=="reviews.php") $status="class='active'"; else $status="";
echo '<li><a ".$status." href="reviews.php">Testimonials</a></li>';
if ($SERVER["PHPSELF"]=="about.php") $status="class='active'"; else $status="";
echo '<li><a ".$status." href="about.php">About</a></li>';
if ($SERVER["PHPSELF"]=="prices.php") $status="class='active'"; else $status="";
echo '<li><a ".$status." href="prices.php">Prices</a></li>';
if ($SERVER["PHPSELF"]=="contact.php") $status="class='active'"; else $status="";
echo '<li><a ".$status." href="contact.php">Contact</a></li>';
}
?>
I found this code but it did not work for me and ideas as to a good method of doing this?
Try adding these lines to the start of your script:
<?php
// highlight all script warnings and errors
error_reporting(-1);
ini_set('display_errors', true);
// remove above two lines after use
if(isset($_SERVER["PHP_SELF"])) {
@mumford1, I think what John meant is that when you add code, you should enclose them in backticks so that they are displayed verbatim without being parsed as was the case with you.
This is the code that should be in your header include file. It’s not the most elegant solution but in keeping with your existing structure, this is probably what will work best for you. I’ve included comments, let me know if anything is unclear.
<?php
// Not sure what you use this for, I'm just including this since you have this in your code although
// it's set to an empty string in every case.
$status = '';
// Initializing variables.
$homeActive = $elecSemActive = $supStuActive = $gcseResActive = '';
$active = 'class="active"';
switch(basename($_SERVER['PHP_SELF'])) {
case 'elective-semester.php':
echo 'Elective Semester';
$elecSemActive = $active;
break;
case 'supporting-students.php':
echo 'Supporting Students';
$supStuActive = $active;
break;
case 'gcse-results.php':
echo 'GCSE Results';
$gcseResActive = $active;
break;
// By default, we assume you will be at index.php (setting $homeActive).
default:
echo 'Home';
$homeActive = $active;
break;
}
?>
<ul>
<li><a href="index.php" <?php echo $homeActive; ?>>Learning Journey <i class="glyphicon glyphicon-menu-right"></i></a></li>
<li><a href="elective-semester.php" <?php echo $elecSemActive; ?>>Elective Semester <i class="glyphicon glyphicon-menu-right"></i></a></li>
<li><a href="supporting-students.php" <?php echo $supStuActive; ?>>Supporting Students <i class="glyphicon glyphicon-menu-right"></i></a></li>
<li><a href="gcse-results.php" <?php echo $gcseResActive; ?>>GCSE Results <i class="glyphicon glyphicon-menu-right"></i></a></li>
</ul>
Also as I have an index.php within the directory it does not highlight that menu item when you land on the page, is their no other more elegant way of acheiving what I am after?