Forum Moderators: coopster

Message Too Old, No Replies

Multiple PHP includes in one include file

Putting header, footer, etc in a single include file

         

cookie2

1:06 am on Nov 21, 2004 (gmt 0)

10+ Year Member



Is it possible to set a file for php to include in an html document that contains multiple things in a single file?

In other words, rather than have separate files of header.inc, footer.inc, menu.inc, etc I'd like to put them all in a single file master.inc.

I know I can call the file to the html page with
<?php
include( $_SERVER['DOCUMENT_ROOT'] . '/includes/master.inc' );
?>

but what I don't know (or even if it's possible) is how to set up and then call the individual elements from the master.inc to the appropriate place in the html page.

Can this be done? If so, how?

Salsa

1:45 am on Nov 21, 2004 (gmt 0)

10+ Year Member



Absolutely. For the examples you gave, in the include file, just make a function for each thing you want it to do; eg:

<?php 
/** master.inc.php **/
function html_header(){
?>
<html>
...
<body>
...
<?php
}
function html_footer(){
?>
...
</body>
</html>
<?php
}
?>

You can open and close the PHP tags any way you wish, just so any PHP code is within the tags.

Then in your output page:

<?php 
/** some_page.php **/
...
include("master.inc.php");
html_header();
echo $content;
html_footer();
?>

When naming include files, use a PHP extension to ensure that the file will be parsed by the server if anyone tries to open it out of context.

I wish you well.

cookie2

3:49 pm on Nov 21, 2004 (gmt 0)

10+ Year Member



Thanks but this doesn't seem to work for me. What am I doing wrong?

/** master.php **/
function html_header() {
Testing to include this header
}

function html_footer() {
Another test
}

**********************
Then in the html page:

<html>
<head>
head stuff
</head>
<body>
yada yada html

<?php
include( $_SERVER['DOCUMENT_ROOT'] . '/includes/master.php' );
function html_header();
echo $content;
?>

more html stuff

<?php
function html_footer();
echo $content;
?>

</body>
</html>

I'm getting a PHP Fatal error: Call to undefined function: html_header() message.
What's happening is the included sections are printing the entire functions on the html page as in:
function html_header() {
Testing to include this
} function html_footer() {
Another test
}

It's printing the entire contents of both functions together including the naming and brackets. It makes no difference if I remove the echo $content line. It prints the same way on the html page with or without it. I've tried putting the call to the include at the top of the page. No change.

Help!

Storyman

4:45 pm on Nov 21, 2004 (gmt 0)

10+ Year Member



Cookie2,

It will work. There is just a minor tweak that you need to make.

You need to move the closing?> in the footer function like this.
<?php } function html_footer(){?>

And in some_page.php delete the line echo $content; since $content has not been defined.

Salsa

5:03 pm on Nov 21, 2004 (gmt 0)

10+ Year Member



In your functions, you need to do one of two things, either
close your PHP tags when you want to display text (or pure HTML), e.g.:

<?php 
...
/** master.php **/
function html_header() {
?>
Testing to include this header
<?php
}
...
?>

... or use PHP's echo or print commands to output the text, e.g.;

<?php 
...
/** master.php **/
function html_header() {
echo "Testing to include this header.<br>\n"; // '\n' is the new line character
print "Writing another line.<br>\n"; // '//' lets you make unparsed comments
}
...
?>

On your "html" page, I think I confused you. I meant only for $content to simply be a variable that you would store/build the main content of your page, like...

<?php  
/** some_page.php **/
/* Important: Your file name needs to end with a .php extension.
Later you can change which file extensions your server will
parse, but for now, make sure that all of your files with PHP
code in them end with .php, not .html. */
...
$content = "Load the variable with the main content of your page...";
include($_SERVER['DOCUMENT_ROOT'].'/includes/master.php');
html_header();
echo $content;
html_footer();
?>

You don't have to use a variable like $content, though. You can just...

<?php  
/** some_page.php **/
include($_SERVER['DOCUMENT_ROOT'].'/includes/master.php');
html_header();
?>
Be sure to close your PHP tag, as above, before adding straight text.
Put some text/html your main content here...
<?php // open the PHP tag before the next PHP code
html_footer();
?>

I hope this helps.

cookie2

9:34 pm on Nov 21, 2004 (gmt 0)

10+ Year Member



I must really be dense. :(

/** master.php **/
function html_header {
?>
Testing to include this header

<?php
} //This is line 7

function html_footer(){
?>
Testing to include this footer

<?php
}

*************************
/** html_page.php **/

<?php
include( $_SERVER['DOCUMENT_ROOT'] . '/includes/master.php' );
?>

<html>
<head>
</head>
<body>
<?php
html_header(); //This is line 118
?>
html yada yada
<?php
html_footer();
?>
</body>
</html>

This throws two errors now:
PHP Parse error: parse error, unexpected '}' in master.php on line 7
PHP Fatal error: Call to undefined function: html_header() in html_page.php on line 118

It's worse than it was before. :(

Salsa

3:11 am on Nov 22, 2004 (gmt 0)

10+ Year Member



Don't get frustrated. You're actually getting closer, not farther away. A programming language like PHP, however, is not as forgiving as the spoken word, and just one misplaced piece of punctuation can throw it into turmoil. The worse news is that PHP doesn't hold for itself the same standards of communication as it holds for you!

For example, sometimes PHP errors don't show up on exactly the line where the error is--only on the line where it figures out that there is an error. For instance, I think your first error is actually before line one!

If:

} //This is line 7

is really line 7, that means:

/** master.php **/

would be line 1. Did you have a PHP open tag (<?php) before that? I'm guessing not, because PHP reported an error on line 7, the first time that you opened the tag in the code that you posted, and the next line was a }, which PHP didn't know what to do with.

Also, if you'd opened the tag before:

/** master.php **/

You would have gotten an error at line three (line two in your code):

function html_header {

When you declare a function, you must include the () after the function name (for holding arguments to the function) even if there are no arguments. So the line should look like this:

function html_header() {

Next, be sure you close the tag (?>) after the the closing html_footer curly brace, }, on line 14, eg:

<?php  
} // line 14
?> // you're missing this line

Your second error message, "PHP Fatal error: Call to undefined function: html_header() in html_page.php on line 118" was simply caused by html_header() not being properly defined as mentioned above, so that error just cascaded to cause an additional error.

That cascading effect is a common thing. At first, you'll type up a new piece of code and feel totally inept because you get 10 error messages when you run it. But, really, it might have been only one typo way up the code that cascaded down, upsetting everything that depended on it.

At this point on your learning curve, I know that getting unfamiliar error messages can be frustrating. Maybe I'm weird, but today I look forward to them! At one time, my goal was to write a piece of code that worked the first time. While that's happened a few times, I now feel more satisfied when I do get an error and am able to recognize it and fix it within a few seconds. Stick with it, and it will happen to you, too.

I wish you well.

Storyman

4:15 am on Nov 22, 2004 (gmt 0)

10+ Year Member



Cookie2,

Salsa is spot on about learning from errors.

My suggestion is to narrow the scipt down to the minimum and add one element at a time. You'll learn a great deal in the process.

cookie2

1:44 pm on Nov 22, 2004 (gmt 0)

10+ Year Member



I guess I thought that the php open and close braces that were in the html page calling the function would also count for the included information. I was wrong. I added the open and close lines to the master.php so the entire include page looks like so:

/** master.php **/
<?php
function html_header () {
?>
Testing to include this header

<?php
}
?>

<?php
function html_footer(){
?>
Testing to include this footer

<?php
}
?>

and this now works without throwing any errors. Yay! BUT... I'm also getting the commented area displaying as well. So the include for the header is showing on my html page as:
/** master.php **/ Testing to include this header

I know that I could remove that comment and the problem would be solved but since php comments aren't supposed to be visible I'm wondering how to correct that the right way now?

Thanks for all your patience and help. I really appreciate it.

tomda

2:03 pm on Nov 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try

[code]
<!-- master.php -->
<?php
function html_header () {
?>
// Testing to include this header

<?php
}
?>

<?php
function html_footer(){
?>
// Testing to include this footer

<?php
}
?>
[code]

The first comment in outside the php_tags, so it should HTML, then <!--blahbalh --> is used.
The second comment are php, just add // at the beginning of the sentence

Salsa

3:13 pm on Nov 22, 2004 (gmt 0)

10+ Year Member



...Or switch your first two lines of code around so they read

<?php  
/** master.php **/

The purpose of the /* */ symbols is to let you include comments to yourself within your PHP code. It's just away to tell PHP to ignore what you've written between them and not to try and parse the comments. The equivalent way to make a comment in html is <!-- -->, as tomda suggested. So:

/* PHP will ignore everything you type here. This 
comment can go on for multiple lines, until you type */

// This is for short comments. It has no ending symbol. 
// The end of the line will automatically end the comment.
// PHP will automatically return to parsing the next line.

Congrats on getting it working!

cookie2

7:00 pm on Nov 22, 2004 (gmt 0)

10+ Year Member



Thank you Tomda and especially Salsa. I tried all of the suggestions you both made and they all worked. Maybe someday I'll be able to spot something like that myself. Duh!

I've now got it working and do appreciate all your help. Thank you all so much.

bloke in a box

10:06 am on Nov 23, 2004 (gmt 0)

10+ Year Member



Don't be discouraged :).

Teaching yourself PHP can be very time consuming, very irritating and upsetting but in the end it can be very rewarding.

Be prepared to spend a lot of time debugging your code for the most stupid of errors, there is no perfect coder out there that doesn't make mistakes so don't worry. :)