3

I am currently developing a website in purpose of learning some more, but I just could not figure this one out and I do not know what to search for exactly, I haven't found anything.

So basically I have a navigation bar, a content box, and a footer. I'd like to divide the website in to three files. That way, I would for example only need to edit one file to edit all links in the navigation bar on ALL pages.

I can simply do this by putting:

<?php include('navigation.php'); ?>

Where I want it to be. But here comes my problem: on each page that I have, my navigation bar should change its active page/tab and highlight it.

My navigation bar looks like this: Home | News | About | Contact

When I click News and land on the news page, it should get highlighted in the navigation bar (through CSS). But how can I achieve this when I have the navigation bar in one single file? Then it would highlight it on ALL pages. This is the problem I currently have and I have no clue if this is even possible in PHP?

Any help is appreciated! Thanks

TyhaiMahy
  • 85
  • 1
  • 2
  • 8

3 Answers3

3

Simplest method: set a global variable to say "where" you are, and have the nav menu check for that:

e.g.

index.php:

<?php
$PAGE = 'home';
include('navigation.php');

navigation.php:

<?php

...
if (isset($PAGE) && ($PAGE == 'home')) {
    .... output "home" link with you-are-here highlight
} else {
    ... output regular home link.
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Really nice! Thank you! I have a question though: What would happen if the user has two browser tabs open, with two different links up? Will the `$PAGE` only contain the latest one visited? Or would two links be highlighted in the nav bar? – TyhaiMahy Jul 06 '15 at 21:30
  • each 'tab' is just like a new user in another country –  Jul 06 '15 at 21:46
  • php doesn't keep running when you're not doing requests. EVERY page request in php is completely independent of each other. you'd have two different $PAGE floating around for the few split seconds that PHP is building the individual pages, and then everything in PHP is gone. the onlyw ay you'd have a conflict between two pages is if you stored the data in a session or something other external system. – Marc B Jul 06 '15 at 21:57
3

You could perhaps check what is the current URL and add active class on your menu items accordingly.

<?php
    $url = basename($_SERVER['PHP_SELF']);    
?>

and then when you generate your menu links, something like this:

<li class='<?php echo ($url == "about.php") ? "active" : ""?>' >About</li>

Something along those lines.

Slavenko Miljic
  • 3,836
  • 2
  • 23
  • 36
0

Get the page from the p URL handle. Check if its allowed, otherwise go to home.

Then check if the page is currently active in the menu, if so; add class active.

<?php
// Get the page from the url, example: index.php?p=contact
$page = $_GET['p'];

// Whitelist pages for safe including
$whitelist = array('home', 'news', 'about', 'contact');

// Page not found in whitelist
if (!in_array($page, $whitelist)):
    $page = 'home';
endif;

include('header.php');
include('navigation.php');
include($page . '.php'); // Include page according to url
include('footer.php');
?>


<ul>
    <li>
        <a href="index.php?p=home" class="<?php if ($page === 'home'): ?>active<?php endif; ?>">
            Home
        </a>
    </li>
    <li>
        <a href="index.php?p=news" class="<?php if ($page === 'news'): ?>active<?php endif; ?>">
            News
        </a>
    </li>
    <li>
        <a href="index.php?p=about" class="<?php if ($page === 'about'): ?>active<?php endif; ?>">
            About
        </a>
    </li>
    <li>
        <a href="index.php?p=contact" class="<?php if ($page === 'contact'): ?>active<?php endif; ?>">
            Contact
        </a>
    </li>
</ul>
vonUbisch
  • 1,384
  • 17
  • 32