Forum Moderators: coopster
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?
<?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.
/** 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!
<?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.
/** 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. :(
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.
/** 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.
[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
<?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!
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. :)