Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Split website in to files [closed]

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

like image 964
TyhaiMahy Avatar asked Feb 12 '26 07:02

TyhaiMahy


2 Answers

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.
}
like image 72
Marc B Avatar answered Feb 14 '26 22:02

Marc B


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.

like image 39
Slavenko Miljic Avatar answered Feb 14 '26 22:02

Slavenko Miljic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!