Wickham Posted December 25, 2008 Report Share Posted December 25, 2008 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. Quote Link to comment Share on other sites More sharing options...
Andrea Posted December 25, 2008 Report Share Posted December 25, 2008 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... Quote Link to comment Share on other sites More sharing options...
Wickham Posted December 25, 2008 Author Report Share Posted December 25, 2008 Good point; my post is simplified for beginners; Stefan's videos take it a stage further. Quote Link to comment Share on other sites More sharing options...
astonj Posted January 10, 2009 Report Share Posted January 10, 2009 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!. Quote Link to comment Share on other sites More sharing options...
Andrea Posted January 10, 2009 Report Share Posted January 10, 2009 hi guys, i have had the same and my solution is to use <?php include 'includes/menu.html'; ?>Which is exactly what Stef's PHP Include Video Tutorials are all about...... Quote Link to comment Share on other sites More sharing options...
LSW Posted January 10, 2009 Report Share Posted January 10, 2009 Sounds like a Sticky to me... let one of us... or me know when you are happy with it and we will slap some tar on that bad boy and splat it on the wall. Quote Link to comment Share on other sites More sharing options...
Wickham Posted January 10, 2009 Author Report Share Posted January 10, 2009 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. Quote Link to comment Share on other sites More sharing options...
astonj Posted January 11, 2009 Report Share Posted January 11, 2009 (edited) yes agreed, i would only include the code for the menu, i would never include the doc type etc Edited January 11, 2009 by astonj Quote Link to comment Share on other sites More sharing options...
LSW Posted January 12, 2009 Report Share Posted January 12, 2009 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. Quote Link to comment Share on other sites More sharing options...
Wickham Posted January 12, 2009 Author Report Share Posted January 12, 2009 OK a new thread has been started; check it and lock it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.