Jump to content

I don't want to edit the menu 100 times


Wickham

Recommended Posts

Here is a FAQ I often get asked by beginners. Please comment so that we can arrive

at an improved answer.

 

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.

Link to comment
Share on other sites

Stef has 2 great video tutorials explaining exactly this over on www.idea22.com: 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...

Link to comment
Share on other sites

  • 3 weeks later...

hi guys, i have had the same and my solution is to use <?php include 'includes/menu.html'; ?>

 

what this means is separate your website into individual elements e.g. header, left content, right content, footer etc

 

But the key here is to create separate pages for each section of your website.

 

And then you simply globalize all of your sections.

 

So you only have to edit that one page, hence 'menu.html' and then as this page has been included in all other pages you only have to make the amends to the menu.html page. Apologies if this is not clear please say if this is not clear enough, but this is a great way to avoid editing your menu 100 times!.

Link to comment
Share on other sites

hi guys, i have had the same and my solution is to use <?php include 'includes/menu.html'; ?>

 

what this means is separate your website into individual elements e.g. header, left content, right content, footer etc

 

But the key here is to create separate pages for each section of your website.

 

Note what I said in my first post:-

 

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 will get duplications all the way through your page, which may display correctly but when you look at the source of the processed online page it will be difficult to sort it all out, and shouldn't be done this way in my view; the include files should only be the minimum code required for repetition.

Link to comment
Share on other sites

You speak here as if this was a Beta for the thread. If you have no changes to it, can you copy it into a new thread and I will sticky that before it gets replies if you just want it, I can lock it. We can use this thread for any comments the reguklars may have on improving it. Just let me know when you're ready to go final.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...