By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,913 Members | 1,379 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Neat way to use Headers and Footers in PHP

P: 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

Expand|Select|Wrap|Line Numbers
  1. <?php if($hf=='header'){ ?>
  2.  
  3. <html>
  4. <head>
  5. <title>Page Title</title>
  6. </head>
  7. <body>
  8.  
  9. <?php } elseif($hf=='footer') {?>
  10.  
  11. </body>
  12. </html>
  13.  
  14. <?php } ?>
  15.  

header.php

Expand|Select|Wrap|Line Numbers
  1. <?php $hf='header'; include('template.php')?>
  2.  

footer.php

Expand|Select|Wrap|Line Numbers
  1. <?php $hf='footer'; include('template.php')?>
  2.  
Mar 26 '08 #1
Share this Article
Share on Google+
7 Comments


P: 1
I think that is not as 'neat' as it could be. And, it is also over-complicating it.

Just make 2 functions like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function page_header() {
  3. echo '
  4. <html>
  5. <head>
  6. <title>Page Title</title>
  7. </head>
  8. <body>
  9. ';
  10. }
  11.  
  12. function page_footer() {
  13. echo '
  14. </body>
  15. </html>
  16. ';
  17. }
  18. ?>
And then just enter either 'page_header()', or 'page_footer()' where you want the headers or footers

Cameron
Apr 3 '08 #2

Markus
Expert 5K+
P: 6,050
I think that is not as 'neat' as it could be. And, it is also over-complicating it.

Just make 2 functions like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function page_header() {
  3. echo '
  4. <html>
  5. <head>
  6. <title>Page Title</title>
  7. </head>
  8. <body>
  9. ';
  10. }
  11.  
  12. function page_footer() {
  13. echo '
  14. </body>
  15. </html>
  16. ';
  17. }
  18. ?>
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
Jun 24 '08 #3

rohypnol
P: 54
Just make 2 functions like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function page_header() {
  3. echo '
  4. <html>
  5. <head>
  6. ...
  7. ...
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
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title><?php if (isset($title)) echo $title; ?></title></head>
  3. <body>
page_xxx.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $title = 'my page';
  3. require('header.php');
  4. if (isset($_POST['name'])) echo 'Hello, '.$name;
  5. require('footer.php');
  6. ?>
footer.php
Expand|Select|Wrap|Line Numbers
  1. </body>
  2. </html>
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.
Jul 29 '08 #4

Markus
Expert 5K+
P: 6,050
@rohypnol
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.
Dec 6 '08 #5

FLEB
P: 30
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:

Expand|Select|Wrap|Line Numbers
  1. <?php function header() { ?>
  2. <html>
  3. <title>Title goes here</title>
  4. <body>
  5. ...Header, header, header...
  6. <?php } ?>
  7.  
  8. <?php function footer() { ?>
  9. ...Footer, footer, footer...
  10. </body>
  11. </html>
  12. <? } ?>
  13.  
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.
Dec 17 '08 #6

dlite922
Expert 100+
P: 1,584
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
Jan 6 '09 #7

code green
Expert 100+
P: 1,726
May I join the discussion.
I agree with rohypnol
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.
Expand|Select|Wrap|Line Numbers
  1. include 'header.html';
  2. .............................
  3. some php.....
  4. include 'webForm.html';
  5. more php .............
  6. 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.
Jan 6 '09 #8