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?
The navbar is usually inserted as the first item within the <body> tag.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With