Reputation: 1205
i am trying to include a php header inside each of the pages i make. Now as far as i know, it can be done by using <?php include('header.php'); ?>
inside each file.
Now my problem is that i have 5 pages, and in the header php i have a nav which links to each of this pages. When i load the header i would like to change the link of the file which the header is loaded on.
Ex: if i open the Home page, i want the header to have the link inside the header nav bolded. That would probably require me to send a variable to the header file when i include it or smt? if so, than how to do that ? :D
Thank you in advance, Daniel!
Upvotes: 1
Views: 1753
Reputation: 1013
In below code, you can call this class to other pages and pass the page name as parameters and can call a jQuery function with that very easily
<?php
class header {
function __construct($title = "Home", $page = 'home') {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script>
$(document).ready(function(){
$('.<?php echo $page ?>').addClass('active');
});
</script>
</head>
<body>
<ul class="nav">
<li class="home"><a href="index.php"><br />HOME<br /></a></li>
<li class="about"><br />ABOUT US<br /></li>
</ul>
<?php }} ?>
Upvotes: 1
Reputation: 57346
One way would be to check the name of the executing script from your header file, something like this (inside header.php):
$page = basename($_SERVER["REQUEST_URI"]);
if($page == 'index.php') {
//header included from inside index.php
}
elseif($page == 'contact.php') {
//header included from contact.php
}
...
Upvotes: 1
Reputation: 31141
You can either do this by setting a variable before including the header or by looking at the URL.
A simplified example:
$page = "home";
include "header.php"
In header.php
if ($page == "home") { ... }
The following code will give you the file name from the URL
$basename = basename($_SERVER['REQUEST_URI']);
So if you have http://mysite.com/index.php
it'll give you index.php
You can use this in your header.php
similarly to how I described above to decide which link to bold.
Upvotes: 2
Reputation: 6221
You can always use $_SERVER['SCRIPT_NAME']
to obtain the name of the most outer php file that was requested. Than you can change style of link in navigation accordingly, ie:
<a href="some.php" style="font-weight: <?= $_SERVER["SCRIPT_NAME"] == '/some.php' ? 'bold' : 'normal' ?>">Link to Some</a>
<a href="other.php" style="font-weight: <?= $_SERVER["SCRIPT_NAME"] == '/other.php' ? 'bold' : 'normal' ?>">Link to Other</a>
Upvotes: 2