1

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!

3
  • What have you tried? Commented Aug 13, 2012 at 14:13
  • I believe you're looking for this solution apaddedcell.com/… Also look up php frameworks Commented Aug 13, 2012 at 14:14
  • edit header.php to use $_SERVER['PHP_SELF'] to get current page and alter the Nav accordingly Commented Aug 13, 2012 at 14:14

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah beat me to it. I would always suggest the latter. Its easier to maintain, and also not as confusing.
wow this is great, i just used this idea and it works great :D Thank you very much for this and also @ Aleks G and Kuba Wyrostek i will try that aswell to see how it works later on when i will try to develop a little framework :D
2

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>

Comments

1

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
}
...

Comments

1

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

1 Comment

this is also great but i'm trying to do more html / css stuff and use jQuery only when i cant do it witohut it. voted up, thank you very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.