Adding active class to navigation items that are within an include file

Hi

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?

Thanks

That code is actually using incorrect variables.
It should be $_SERVER['PHP_SELF']. See http://php.net/manual/en/reserved.variables.server.php
But even that variable is not the right one to use in this instance.

Can you tell us what your URLs look like?

Unfortunately @mumford1 did not enclose the script within leading and trailing three back ticks resulting removing the under_scores.

Example NOT using three back ticks:
if(isset( $_SERVER[“PHP_SELF”]) ) {

}


Example using three back ticks:

if(isset( $_SERVER["PHP_SELF"]) ) {
...
}

Edit:
Other options are available by highlighting the text and selecting the relevant icon.

1 Like

Hi

This is what is in my include file looks like

<ul>
		<li><a href="index.php" class="active">Learning Journey <i class="glyphicon glyphicon-menu-right"></i></a></li>
		<li><a href="elective-semester.php">Elective Semester <i class="glyphicon glyphicon-menu-right"></i></a></li>
		<li><a href="supporting-students.php">Supporting Students <i class="glyphicon glyphicon-menu-right"></i></a></li>
		<li><a href="gcse-results.php">GCSE Results <i class="glyphicon glyphicon-menu-right"></i></a></li>
</ul>

Thanks

Here is the code again I have, don’t have to use this if their is another way?

<?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>';
}
?>

The above does not work

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"])) {

Hi

I get this:

Parse error: syntax error, unexpected end of file in /home/public_html/essa/our-curriculum/our-curriculum-navigation.php on line 45

But once I remove the bit you said to add I no longer get any errors?

Thanks for that @John_Betong.

@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>
1 Like

Hi

This is what I have added

<li>
    <a href="index.php?action=index" class="<?= isset($_GET['action']) && $_GET['action'] == 'index' ? ' active' : '' ?>">Learning Journey <i class="glyphicon glyphicon-menu-right"></i></a>
</li>

Thing is it produces this in the URL

index.php?action=index

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?

I would really appreciate any further help.

Many Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.