I want to modularize html page elements using PHP. This is the code I am currently using:
<?php include("template.php");
echo "Hello";?>
The template.php actually also includes the body of original html page, inside of which I wish to write "Hello". So will I have to create 3 different PHP templates: 1 for header, 1 for navigation, and 1 for footer and then include them at appropriate locations in a new PHP file or is there any other way out?
You could include the 3 files on each page?
<?php include("header.php"); ?>
<?php include("navigation.php"); ?>
Hello
<?php include("footer.php"); ?>
You could also include the navigation.php inside the header.php
How about using a template engine?
If you want to have same complex layout for multiple pages with some variations on the datasources a template engine would be of real help.
Some good template engines for php are:
Smarty http://smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions
Mustache PHP https://github.com/bobthecow/mustache.php
PHP TAL http://phptal.org/
Of course if you just want some simple header and footer - use include_once as suggested by the other answers.
I suggest you use the include_once() function to include the scripts you want to insert and place the code into html <div></div> tags so you can display wherever you need them to display in the web page.
<div>
<?php include_once("header.php")?>
</div>
<div>
<?php include_once("body.php")?>
</div>
<div>
<?php include_once("footer.php")?>
</div>
Related
I'm making a website with multiple pages with About info, Projects, Events etc.
My first question is, how can I include my nav bar, header and footer to be in all of the html files without copying the full code for each of them?
My second question is, how should the html structure look in the separate header/footer files so I can include them in my pages html's?
If you mention PHP as a solution, please point me to some link/tutorial for the process.
If you use php files you could do something like this:
index.php:
<?
include("header.php");
?>
//page content
<?
include("footer.php")
?>
header.php:
<head>
//head
</head>
<body>
footer.php:
<footer>
//footer
</footer>
</body>
In the header.php you could put the tags etc.
In every page you could just include these files than
You can create specific files for header and footer and require them in the pages you need them.
require 'footer.php'
require 'header.php'
First you need to make three files navbar.php, header.php and footer.php which contains their code separately.
Now in your each web pages place the below code.
include('header.php'); //Your website header code will be load from header.php
include('navbar.php'); //Your website navbar code will be load from navbar.php
include('footer.php');//Your website footer code will be load from footer.php
You can do with AngularJS. If you do not want to use server side scripting
http://www.w3schools.com/angular/
<body ng-app="">
<div ng-include="'header.htm'"></div>
Your page goes here
</body>
do not forget to add AngularJS library or you can use cdn.
I am making website and this website will have more than 20pages.
I am using my template to add a webpage. This template has header and footer, so I just add the body of new page.
But, what if I want to change navigation bar in the header, then I have to change all 20 pages that I already made to correct.
I want to know better way.
I read a book, and it says about "php include" function.
Should I use this function in the header and footer of each webpage to call header and footer file?
If I want to change the navigation bar in the header, all I can do is changing only one header file, then rest of website will be changed.
Is this correct way?
In this case, what do you do?
I am a beginner, so please advise me.
Thank you in advance.
You should build your website as following. The header.html would contain the navigation.
header.html
<!doctype html>
<html>
<head>
...
</head>
<body>
<nav>
...
</nav>
footer.html
<footer>
...
</footer>
</body>
</html>
page.php
<?php
require_once 'header.html';
?>
Your content goes here
<?php
require_once 'footer.html';
?>
You should use require_once so the header and footer will be imported only once per script and if the header or footer cant be found, the script will throw an exception and stop the "application".
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!
Yes this is the correct way or you could just copy your header and footer code to every .html file (if you don't like PHP)
Yes, this is the correct way. Try to think of the DRY principle - don't repeat yourself. Elements of your web page that are common across multiple pages can be coded once, then called in. If you need to update these elements, you update them once and it affects all pages.
You then "include" these elements into your page, and the elements are self contained files. As a basic example you would have header.php
<html>
<head><!-- all of your head meta tags in here--></head>
<body>
<div id="header"><!--your header elements and top menu in here --></div>
and footer.php:
<div id="footer"><!-- your footer elements in here--></div>
</body>
</html>
Don't forget in both header and footer files you can then put dynamic code if you wish. Then for each of your pages you would simply call these files in using include, include_once, require or require_once
<?php
include('header.php');
//this is where your actual page content goes
include('footer.php');
?>
A very basic example, but hopefully that makes sense to you.
Correct.
If you include another php file it will calculate/ask for input/do output (whatever you do in the file) as part of your main file.
If you include a php file that has a function you can later call this function without it showing in your main file. (Saves space in your main file)
So your 20 pages may only need a few lines of body text and the rest is header and footer. This will make changes very easy
I want to make a header file, but am not experienced with PHP in almost any aspect. My uncle was telling me that I can use a header file with PHP, and that it was like a CSS with HTML.
The following is the HTML I want in my PHP:
<center><nav>
<ul>
<li>Home</li>
<li>Arcade
<ul>
<li>Action</li>
<li>Arcade</li>
<li>Puzzle</li>
<li>Vehicle</li>
<li>Violence</li>
<li>Defense</li>
<li>RPG</li>
</ul>
</li>
<li>Watch
<ul>
<li>TV Shows</li>
<li>Movies</li>
</ul>
</li>
<li>Extras
<ul>
<li>News</li>
<li>Updates</li>
</ul>
</li>
<li>Support</li>
</ul>
</nav></center>
How could I incorperate that into a header file?
I have looked at other websites such as W3Schools, but they don't seem to work. I think my main issue is that I am not sure how to put the HTML into the PHP document. When I do it, I try to type out the HTML as if it were header.html, only, ya know, it's not.
But anyways, if anybody could even just point me in the right direction of how to incorperate that HTML I included into a header.php file, and what to put in the html.
From what I have seen, the best scenario for including the PHP document in the HTML code is:
<html>
<?php
header('Location: http://www.example.com/');
?>
Right now, I'm putting that HTML in every single page. This is restricting me from making navigational tab changes!
My website is http://www.gameshank.com/
Again, thanks in advanced, sooo much!
Ah... the PHP header command is used to output a HTTP header, so I'm really not sure what you're hoping to achieve via its use.
What you want to do is save your generic header HTML/PHP code into a new PHP file (perhaps called "header.php") and then include the contents of that file within each of your existing PHP pages via an include statement. For example:
<html>
<head><title>Sample HTML page</title></head>
<body>
<?php include 'header.php'; ?>
<h1>Page specific content</h1>
<p>And stuff.</p>
</body>
</html>
By doing this, each page will automatically contain the contents of the header.php file (which will appear wherever you place the include statement) and any changes you require can simply be made to the header.php file.
You definitely can!
On your main page (Lets say index.php), on the top you can put in
<html>
<?php
include 'header.php';
?>
</html>
(Or whatever your header file is called.).
Put that HTML in header.php you want to include.
You have to use include like this
<?php
include('header.php');
?>
or use require
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
While you're at this, it's also a pretty good idea split the footer off of your page and place it in a seperate PHP file like you're doing with your header.
The point of this is it allows you to make changes to code that is featured in multiple pages throughout your site without having to go through every single page and make the same change.
For instance if you have "Copyright (c) 2013 Blah Blah Blah" at the bottom of 50 pages on your site, when 2014 rolls around you don't want to edit 50 pages to change 2013 to 2014. You'd just make it once in "footer.php".
And like the others have said, simply include the file in your pages.
<?php
include 'footer.php';
?>
I'm looking for ways to have my pages search for the page layout from an external template page. Please see the below example.
<head>
<title></title>
</head>
<body>
<search for header, css, layout, etc from external page>
Page contents
<search for footer>
</body>
Is there any way to do this using PHP or HTML? I want to be able to edit the layout for all the pages without having to do it page by page. I welcome any other means to achieve the same effect as long as it works on all the browsers.
Thank you very much!
This is exactly the sort of thing that PHP is for. A PHP script can include the contents of another script using the include statement.
So each page in your application could have an associated PHP script that generates the contents, and includes footer.php for the footer layout. In this way, when you change footer.php all the pages that use it will automatically get the changes.
You can't do this with pure HTML, though you could with some javascript and Ajax.
Like Andrew said, use includes. I'll set up 2 basic examples.
The simplest, have multiple layout files that are called by your main file(s):
header.php:
<div id="header">
Menu can go here.
<?php echo 'I make all my files .php, so they can use PHP functions if needed.'; ?>
</div>
footer.php
<div id="footer">
Footer Link
</div>
index.php
<html>
<head></head>
<body>
<?php include('/path/to/header.php'); ?>
Specific index.php content here.
<?php include('/path/to/footer.php'); ?>
</body>
</html>
The other option is to have one PHP file which includes all your different layout elements in functions. The reason I like this, is because you can include one file and then call specific functions for different parts. This can also be used to pass variables like a title of a page.
layout.php
<?php
function makeHeader($title) {
return 'My title is: '.$title;
}
function makeFooter() {
$html = '
<div id="footer">
Footer Link
</div>
';
return $html;
}
?>
index.php
<?php include('/path/to/include.php'); ?>
<html>
<head></head>
<body>
<?php echo makeHeader('Page Title'); ?>
Specific index.php content here.
<?php echo makeFooter(); ?>
</body>
</html>
Just make sure you use relative paths (no http://www.) when including files. This will allow variables and functions to transfer over smoothly. The easiest way to do this is using the PHP variable $_SERVER['DOCUMENT_ROOT'] so if you have a file http://mysite.com/includes/layout.php, you could include it with include($_SERVER['DOCUMENT_ROOT'].'/includes/layout.php') no matter where your file you are including from is located.
I know this is a basic PHP question, and I'm trying to learn the stuff. I very familiar with HTML, CSS and familiar with the CONCEPT of PHP, but not with specifics.
I have always partnered with a back end developer to accomplish this stuff and to set up wordpress sites, etc.
I'm building a very basic four or five page website (a showcase for the client's custom fishing rods: http://www.tuscaroratackle.com/index2.php). I want to call the page header (as in logo, navigation, etc., not as in the head element) dynamically from a php file, and same thing with the footer, so I don't have to rewrite all the markup on every page for these bits.
I don't intend to use a database for this site, I was just thinking I could call those two bits from another file, as you would in a wordpress setup with the header.php in the wp-content directory.
Is there an easy explanation on how to do this? (I get the basics, just looking for help on the more specific PHP calls that need to be made)
Or, if this is not an answer somebody could easy give, can you point me to a good resource to research it further?
Thx
You betcha - include and require -
using include
in your page:
<body>
<?php include 'header.php'; ?>
in your header.php
<div id="header">
<!-- content -->
<?php echo "run php stuff too"; ?>
</div>
would result in:
<body>
<div id="header">
<!-- content -->
run php stuff too
</div>
You should put the header html code in some file such as header.php and then include it with php like:
include ('header.php');
You should specify the correct path there, for example, if you put the header.php file in includes folder, you can include it like:
include ('inclues/header.php');
More Info:
http://php.net/include
Put in a separate file and use include, require or require_once.
Eg
require_once("path/to/myfile.php");
Look into PHP includes.
The way I normally do it is to create a file called includes.php with two functions header() and footer(), then call them on each page like such:
includes.php:
<?php
function header(){
echo '<div id="header">Welcome</div>';
}
function footer(){
echo '<div id="footer">Goodbye</div>';
}
?>
index.php:
<?php
include_once('includes.php');
header();
echo '<div id="content">Main page body</div>';
footer();
?>