Neat way to use Headers and Footers in PHP 
March 26th, 2008, 05:07 AM
| | Newbie | | Join Date: Mar 2008
Posts: 1
| |
I found that I couldn't edit my page easily using programs like dreamweaver after I had divided the template into header and footer files.
Using this method you can load 'template.php' into your visual designer and edit it no probs. Note:Examples below are basic but you get the point
template.php -
<?php if($hf=='header'){ ?>
-
-
<html>
-
<head>
-
<title>Page Title</title>
-
</head>
-
<body>
-
-
<?php } elseif($hf=='footer') {?>
-
-
</body>
-
</html>
-
-
<?php } ?>
-
header.php -
<?php $hf='header'; include('template.php')?>
-
footer.php -
<?php $hf='footer'; include('template.php')?>
-
Last edited by Markus; December 6th, 2008 at 03:10 PM.
Reason: Fixed [code] tags.
| 
April 3rd, 2008, 02:46 PM
| | Newbie | | Join Date: Apr 2008
Posts: 1
| | | re: Neat way to use Headers and Footers in PHP
I think that is not as 'neat' as it could be. And, it is also over-complicating it.
Just make 2 functions like this: - <?php
-
function page_header() {
-
echo '
-
<html>
-
<head>
-
<title>Page Title</title>
-
</head>
-
<body>
-
';
-
}
-
-
function page_footer() {
-
echo '
-
</body>
-
</html>
-
';
-
}
-
?>
And then just enter either 'page_header()', or 'page_footer()' where you want the headers or footers
Cameron
Last edited by Markus; December 6th, 2008 at 03:11 PM.
Reason: Fixed [code] tags.
| 
June 24th, 2008, 11:55 PM
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,862
| | | re: Neat way to use Headers and Footers in PHP Quote: |
Originally Posted by meep I think that is not as 'neat' as it could be. And, it is also over-complicating it.
Just make 2 functions like this: - <?php
-
function page_header() {
-
echo '
-
<html>
-
<head>
-
<title>Page Title</title>
-
</head>
-
<body>
-
';
-
}
-
-
function page_footer() {
-
echo '
-
</body>
-
</html>
-
';
-
}
-
?>
And then just enter either 'page_header()', or 'page_footer()' where you want the headers or footers
Cameron | Agreed.
I'm not understanding the whole use of $hf | 
July 29th, 2008, 02:57 PM
|  | Member | | Join Date: Dec 2007
Posts: 54
| | | re: Neat way to use Headers and Footers in PHP Quote: |
Originally Posted by meep Just make 2 functions like this: - <?php
-
function page_header() {
-
echo '
-
<html>
-
<head>
-
...
-
...
| Please don't put HTML code in PHP... I can hardly stand to see PHP code in HTML, sometimes it may be OK, but please never ever put HTML code in PHP. It's a very bad practice, unfortunately very common. The best way to do it is using a templating engine such as Smarty but if you don't want to get through the hassle of working with Smarty because you're just learnign PHP, use <?php echo $var; ?> inside your HTML. It will keep things a lot cleaner and working with the HTML will be a lot easier.
The best way I've found to use headers and footers in PHP without a templating engine may something like this: header.php - <html>
-
<head><title><?php if (isset($title)) echo $title; ?></title></head>
-
<body>
page_xxx.php - <?php
-
$title = 'my page';
-
require('header.php');
-
if (isset($_POST['name'])) echo 'Hello, '.$name;
-
require('footer.php');
-
?>
footer.php
Any page you have goes in stead of page_xxx.php;
header.php and footer.php should be included in all (or most of :) your files.
| 
December 6th, 2008, 03:15 PM
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,862
| | | re: Neat way to use Headers and Footers in PHP Quote:
Originally Posted by rohypnol Please don't put HTML code in PHP... I can hardly stand to see PHP code in HTML, sometimes it may be OK, but please never ever put HTML code in PHP. It's a very bad practice, unfortunately very common. The best way to do it is using a templating engine such as Smarty but if you don't want to get through the hassle of working with Smarty because you're just learnign PHP, use <?php echo $var; ?> inside your HTML. It will keep things a lot cleaner and working with the HTML will be a lot easier.
The best way I've found to use headers and footers in PHP without a templating engine may something like this: header.php - <html>
-
<head><title><?php if (isset($title)) echo $title; ?></title></head>
-
<body>
page_xxx.php - <?php
-
$title = 'my page';
-
require('header.php');
-
if (isset($_POST['name'])) echo 'Hello, '.$name;
-
require('footer.php');
-
?>
footer.php
Any page you have goes in stead of page_xxx.php;
header.php and footer.php should be included in all (or most of :) your files. | If someone is a newbie, I don't see the problem with this coding style. They'll learn to separate the two later on in their learning.
| 
December 17th, 2008, 11:43 PM
|  | Newbie | | Join Date: Aug 2008
Posts: 30
| | | re: Neat way to use Headers and Footers in PHP
Using the if/thans is a bit much, but using an echo doesn't achieve the goal evilrevolution was talking about : making the editor colorize/WYSIWYG the HTML as HTML, not as a long PHP string.
By coming out of PHP mode, the HTML code goes back to being colorized/WYSIWYG'd with HTML rules, not PHP rules.
However, it could be just as easily achieved by: -
<?php function header() { ?>
-
<html>
-
<title>Title goes here</title>
-
<body>
-
...Header, header, header...
-
<?php } ?>
-
-
<?php function footer() { ?>
-
...Footer, footer, footer...
-
</body>
-
</html>
-
<? } ?>
-
The HTML, while logically wrapped in a PHP function, is not within a <?php ?> block, and is not being interpreted (by the WYSIWYG/code-coloring editor) as PHP code.
| 
January 6th, 2009, 06:22 AM
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,075
| | | re: Neat way to use Headers and Footers in PHP
I think smarty is the way to go. i don't care how new you are, smarty is easy to use.
<vent>
Besides you should get into classes objects and security as soon as possible. It pains me to sift through tens and hundreds of /scripts/ of PHP from people who actually are responsible for giving PHP a bad name.
PHP is a beautiful language with a bad rep from newbies making insecure, vulgar, and to be blunt horrible code. I was there. 4 years ago I didn't know much PHP at all. I learned OOP first, then dissected PHP applications, then started coding and solving first hand problems.
I despise the sight of PHP in HTML now. Your HTML file should not contain no logic other than one or two if and loop statements. No assignment and declarations. NO SQL! No data validations and most importantly no access to variables that have not passed through your PHP script (ie doing something like $_POST['foo'] on a page).
</vent>
Dan
| 
January 6th, 2009, 09:54 AM
|  | Expert | | Join Date: Mar 2007 Location: England
Posts: 1,063
| | | re: Neat way to use Headers and Footers in PHP
May I join the discussion.
I agree with rohypnol Quote: |
Please don't put HTML code in PHP
| I handle Headers and Footers the same way I handle all HTML within PHP by using include. - include 'header.html';
-
.............................
-
some php.....
-
include 'webForm.html';
-
more php .............
-
include 'footer.html';
All HTML is neatly out of the way in their own files.
The only HTML I have never succesfully placed in a HTML file is a
drop down <select> created on the fly within a loop from a database query.
This is better placed in a function.
I have nothing against meep's function idea, in fact a function will parse a lot quicker than include if faster parsing is needed.
I would dump all HTML functions in a seperate file say htmlFunctions.php and
include this at the beginning.
But I think the OP's real problem was reading his code in Dreamweaver,
may I suggest phpDesigner.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|