Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Include Nav-bar For Every Page Techniques?

Tags:

html

css

php

navbar

I'm trying to clean up my code in my php web pages by including files to consolidate each page's code's like so:

File: head.php

<!DOCTYPE html>
<html>
<head>
<title> *Dynamic Page Title* </title>
<meta name"description" "Dynamic Description">
<link href="#">
</head>

File: footer.php

<footer>
   <ul>
      <a href="#"><li>Home</li></a>
      <a href="#"><li>Page 1</li></a>
      <a href="#"><li>Page 2</li></a>
   </ul>
</footer>
<script src="#">
</body>
</html>

File: navbar.php

<header class="navbar navbar-default navbar-static-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <a href="#" class="navbar-brand"></a>
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></button>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a href="index">Home</a></li>                    
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">FAQ</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Contact Us</a></li>
            </ul>
        </div>
    </div>
</header>

File: index.php

<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Home has an active class
<?php include('footer.php'); ?>

File: Page-1.php

<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Page 1 in nav-bar has an active class
<?php include('footer.php'); ?>

You guys get the main idea. I would like tips on how to add active classes in different pages in the nav-bar, and how to add different titles and meta tags to each page.

Any help?

like image 415
alexel Avatar asked Feb 17 '16 15:02

alexel


People also ask

Do you put navbar in header or body?

The navbar is usually inserted as the first item within the <body> tag.

How navigation within files is achieved in PHP?

You'll do this by creating a varible called $thisPage and assigning a value that is both descriptive and unique to the document. Since PHP is a server-side language, the server will take care of the naming of the document and the inclusion of navigation. php well before the the file is sent to the browser.

How do I navigate in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.


1 Answers

You can define variables in your files:

index.php:

<?php $title = 'Home'; ?>
<?php $metaTags = 'tag1 tag2'; ?>
<?php $currentPage = 'index'; ?>
<?php require_once('head.php'); ?>
<?php require_once('navbar.php'); ?>
<body>
Index Page, Home has an active class
<?php require_once('footer.php'); ?>

Then use these variables in the included file:

head.php:

<!DOCTYPE html>
<html>
<head>
<title><?php echo($title); ?></title>
<meta name"description" "<?php echo($metaTags); ?>"><!-- note that this code is wrong -->
<link href="#">
</head>

navbar.php:

<header class="navbar navbar-default navbar-static-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <a href="#" class="navbar-brand"></a>
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></button>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li <?php if ($currentPage === 'Home') {echo 'class="active"';} ?>><a href="index">Home</a></li>                    
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">FAQ</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Contact Us</a></li>
            </ul>
        </div>
    </div>
</header>

In this last part, it's easier to use an array instead of repeating the same code for each line:

<ul class="nav navbar-nav navbar-right">
    <?php
        // Define each name associated with an URL
        $urls = array(
            'Home' => '/',
            'Page 1' => '/page1',
            // …
        );
        
        foreach ($urls as $name => $url) {
            print '<li '.(($currentPage === $name) ? ' class="active" ': '').
                '><a href="'.$url.'">'.$name.'</a></li>';
        }
    ?>
</ul>
like image 188
A.L Avatar answered Oct 03 '22 14:10

A.L