PHP header and footer inclusion - HELP PLEASE

Hey guys,

So I haven’t done any php in a good 15 years and need to use some for my website. I would like to have the following:

Each page will look like this (this is what I am labelling “home.php”):

[php]<?php include (‘template.php’);

echo “$header”; ?>

Is this going to work. <? echo "$footer";

?>[/php]

I was then hoping to have a template.php where all the code is stored like the following:

[php]<?php

$header = ’

Untitled 1 .auto-style1 { background-color: #2F2F2F; } .auto-style2 { background-color: #C0C0C0; background-image: url('images/upper_page.jpg'); } .auto-style3 { border: 1px solid #000000; } .auto-style4 { text-align: right; background-image: url('images/header.jpg'); } .auto-style5 { text-align: center; background-color: #2F2F2F; background-image: url('images/middle_page.jpg'); font-family: Calibri; } .auto-style6 { background-color: #2F2F2F; background-image: url('images/lower_page.jpg'); } .auto-style7 { font-family: Calibri; } .auto-style10 { text-align: center; background-color: #C0C0C0; } .auto-style11 { text-align: left; } .auto-style13 { border-width: 0px; margin-right: 0px; } .auto-style14 { text-align: center; }






Home SERVICES products pictures contact


 


';[/php]

[php]$footer = ’  



















 












 
Facebook

Instagram

 
';

?>[/php]

I keep getting errors… can someone please give me a hand?

Cheers,

Scott

  • Mod added code tags

What errors? We are not going to dissect your code trying to find them. And next time use the code tags.

  • I added the code tags. Can you see your error now?

sorry virgin

oh ya edited ur post. yeah appreciate it!

Purely as a practice for my own coding experience I’ll try to help you out:

[php]echo “$header”; ?>[/php]
[php]echo “$footer”; ?>[/php]

You don’t need quotes as they are variables.

For your template.php it might be handier to have a function. E.G. something like this:

[php]/* REUSABLE CODE */
function addheader(){
echo “html stuff”;
}
[/php]

Then in the files where you want to include this data you would just need a simple function call like this (provided that you have included your template.php in that file):

[php]<?php addheader(); ?>[/php]

Personally, I wouldn’t use php/html/css all in one jumble like that. I like to have php only when it’s really necessary for functionality. Otherwise it just makes everything a bit more confusing as your files get larger and larger.

Speaking of functionality, that’s why I would use a function. Let’s say you want to add some kind of extra task to this header later on - like some kind of math or something. Instead of messing around with a huge string that has lots of oddly spaced data, you can just add a line to your function.

When you echo all of that code you definitely have to make sure that you don’t have double quotes, single quotes, spaces, and all of that other stuff flying around on it’s own. Double quotes and single quotes have very specific functions and they aren’t always interchangeable. In practice they should never be considered interchangeable, I’d wager.

You can read a lot more about that in the php manual about strings. (can’t post a link 'cause I haven’t been here long enough) but it’s easy to find.

Good luck!

You don’t put the headers and footers in functions. You include the files.

[php]include(’./includes/header.php’);

include(’./includes/footer.php’);[/php]

I had a sneaking suspicion something would be wrong with my methods :-\

I’ve been including a config.php into all of my files and then placing all of my functions in there. That way I can use the functions wherever I need them and I only have to remember to include config.php in my files.

Is this not a viable method? Apologies to OP, I won’t reply any further as I don’t want to hijack the thread.

[php] echo “$header”; ?>

<? echo "$footer"; ?>[/php]

Those are issues anyway,

Sponsor our Newsletter | Privacy Policy | Terms of Service