473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Neat way to use Headers and Footers in PHP

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
7 8272
meep
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
6,050 Expert 4TB
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
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
6,050 Expert 4TB
@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
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
1,584 Expert 1GB
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
1,726 Expert 1GB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Web Surfer | last post by:
www.authoring.stylesheets] Is it possible to use stylesheets to override the Headers and Footers that are displayed on a printed webpage ?
2
by: Rupe | last post by:
Hi CSS gurus! I have an embarassing problem. I am a php programmer and I try to expound the advantages of css on the php newsgroups. However, after telling someone that they should use css...
28
by: reneecccwest | last post by:
hello, how can I remove IE headers and footers when I print a page? I'd like to use a code to remove them, not thru the IE page setup. s/RC
3
by: Chris Transcend | last post by:
Hi all, I'm writing my first .NET website and am wondering what the best method for including headers and footers is. I was thinking either a user or server control but am not sure which would...
1
by: Kenneth | last post by:
Hi, In IE6 you can configure output to the printer getting a clean printout only with content, but without headers and footers. You remove the Header and Footer content from the Page Setup. But...
1
by: Dan Sikorsky | last post by:
Under the File menu, Page Setup item in the IE is a way to setup the Page headers and footers the show when you print a page in the browser. How can we set those programmatically in a webform? ...
7
by: Robert Adkison | last post by:
I need to print a web page. It is my preference that my users just do a File/Print from explorer. That way my users will get the print dialog that will allow them to select the fax printer. The...
1
by: Joao_H | last post by:
I am trying to save and print reports of calculations of a program that make adjustments. Doing that for a Word Document is fine because we can format the text from VB .NET. I have already all...
3
by: Dessip | last post by:
Hey, this is probbly a really simple answer, but im useing ASP.NET V1.1 and im trying to create a dynamic header, but a static footer. But i dont want to use place holders. thou its probbly the...
0
by: steve | last post by:
I am looking for a way to create dynamic headers and footers in a web form. There are .Net PDF products that convert web form content into PDF format and insert dynamic headers/footers. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.