Navigation through index.php template?
PHP No Comments »A question I get emailed alot is “How do you do your Web site navigation all through your index.php file?” - or something along those lines. And the answer is really simple.
The dynamic navigation is quite simply a tiny bit of PHP code. I’ll talk you through it:
- Create an index.php page, this is your “template” and should include everything you want on every one of your pages (e.g. a menu, banner, header, footer etc).
- Where you want your page content to go (usually in the centre, below the header), put the following code (in your index.php):
< ?php $page = trim(addslashes($_GET['page'])); if(!$page) $page = "home"; if(!file_exists($page.".html")) include("error.html"); else include($page.".html"); ?> - Upload your index.php page.
That piece of code will simply include the “page.html” where page is the page you pass to the URL. If the page is not available, error.html will be shown. So to call the “script”, you would simply go to www.yoursite.com/index.php?page=XXX
Also, if no page is specified, it defaults to home.html instead of error.html
The new CJ Website Design can (temporarily) be viewed here: http://www.cj-hosting.com/cjdesign - it’s still in early development but should be up in the next week or so due to us moving servers. As you can see from it, we use a different technique on the new site - we use Apache URL Re-Writing techniques to literally re-write the URL to index.php?page=XXX format - pretty neat and search engine friendly, you might want to read more about this technique at the following sites:
- http://www.sitepoint.com/article/search-engine-friendly-urls/
- http://www.devarticles.com/c/a/Web-Services/Make-Dynamic-URLs-Search-Engine-Friendly/
- http://www.devshed.com/c/a/Apache/Search-Engine-Friendly-URLs-with-mod-rewrite/
I hope this answers the question!