Jump to content
Killersites Community
Sign in to follow this  
Wickham

Repeating sections of code on many pages

Recommended Posts

Question:- I've got a site with 100 pages and I don't want to edit the

menu on every page.

 

Answer:- Form one page complete in the normal way with the menu and

check that it validates and displays properly.

 

Open up Notepad or a text editor (not Word) and cut the code that

repeats in every page completely (as much or as little as you want,

without doctype, html, head or body tags) and paste it into Notepad and

save as an "include" file called menu.inc. You then have only one file to

edit if the menu changes.

 

Substitute the code you have cut out with PHP code

<?php include ("menu.inc"); ?>

in the main file and save it with .php filename extension instead of .html

or .htm.

 

Do the same with other main pages where the menu repeats.

You can use .txt, .php, .html or .htm for the "include" file but it

distinguishes it as an "include" file in your file list if you use .inc as the

filename extension and it's even better if you put all "include" files in a

different directory/folder.

 

If you use a complete page as an "include" file with .html or .htm for the

filename extension the final processed page will have two doctypes, two

html tags, two head sections, etc. in an unusual place which is not a

good idea.

 

You can have different "include" files on the same main pages for header

and footer and even use "includes" for the parts of the head section that

repeat.

 

The PHP code needs to be processed by a server before the result can

be seen, either by the hosting service's server just before downloading or

by a server like WAMP (which includes Apache) on your computer for

local viewing.

 

SHTML works the same way but the "include" file must have .txt

filename extension and the main pages have the code and the .shtml filename extension.

 

Check that your hosting service supports PHP or SHTML as free

webspaces provided by ISPs usually do not.

 

Stefan has 2 great video tutorials explaining exactly this in the PHP tutorials: PHP includes part 1 and PHP includes part 2.

 

Not sure if this is in the videos - but it's generally a good idea, especially if the site is set up with sub-folders, to use the absolute paths pointing to the include files, and for anything the include itself points to - that can save a lot of wondering why something suddenly isn't showing up.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×