PHP Navigation

PHP NavigationIn this tutorial, you will learn how to make it extremely easy to edit your site, by putting your layout in only one file, and your content into separate files. You should probably back up your files before you start because if you mess up badly then you won't have to redo your site all over again.

Find a file with your layout in it. This is probably index.html or index.php. If it isn't called index.php, change it.

Now find where your content starts and ends, highlight it, and cut it. (Ctrl+X)

Paste it into a new text document and call it news.htm or something. This file (and all the rest of your content files) don't have to have a *.php extension.

Now, copy (Ctrl+C) the following lines of code and paste them into where your content just was:

<?php
switch($id) {
default:
include('news.htm');
break; case"what1":include('whatever1.htm');
break; case"what2":include('whatever2.htm');
break; case"what3":include('whatever3.htm');
break; case"what4":include('whatever4.htm');
break; case"what5":include('whatever5.htm');
} ?>

The file names in the include() are the filenames of your pages. The black what1, what2, etc names are what you're going to link to later. "Default" is the 1st page that comes up when you go to index.php. For me, this is my news page, news.php. For you it could be news.htm or intropage.html or whatever.

If you have more pages, which you probably do, you can just copy the last line of code with corresponding names (the lines with what5 and whatever5.htm,) and change it to the name of your other pages, then add it to the bottom of the code. So, if you had a page named "contactinfo.htm" and you wanted the name you link to to be "contact" you would first delete all the layout stuff that you had in contactinfo.htm, and leave just the content, then add this to the end:

break; case"contact":include('contactinfo.htm');

Ok, now you can proceed to go through all your pages, delete the layout parts, and leave only the content. For each page remember to add the above code into index.php, and change the variables and filenames to what you need. If you don't then this whole thing won't work for you, and that would definitely suck. Keep in mind that you don't need to have the <html></html>, <head></head>, <title></title>, <body> </body> tags on your content pages.

Wow, that was fun wasn't it? Now, we are going to make the actual navigation. Ok on index.php, (your layout page,) find where your links to all of your pages are. Now change the url to your pages to the black names that you made earlier. For example:

<a href="news.htm"> should become
<a href="?id=default">

<a href="whatever2.htm"> should become
<a href="?id=what2">

Etc, etc. Make sure that the names after ?id= in this section correspond to the names in the big section of code that you made earlier!!!

Repeat the previous step for all your links.

Upload all your files, and test the links! If you followed this tutorial exactly, it will work!

Example:

index.php:

<html>
<head><title>Your Title</title></head>

<body>

Navigation:

<a href="?id=default">News</a>
<a href="?id=what1">Whatever1</a>

<br /><br />

<?php
switch($id)
{
default:
include('news.htm');
break; case "what1":include('whatever1');
break; case "what2":include('whatever2');
}
?>

</body>
</html>

News.htm:

CONTENT CONTENT CONTENT CONTENT

Whatever1.htm:

CONTENT CONTENT CONTENT CONTENT

Whatever2.htm:

CONTENT CONTENT CONTENT CONTENT

Related Articles

  • Navigation Using Switch
    A safer, manual way of navigation using PHP
  • Good Website Navigation
    Website Navigation is defined as the science and skill applied to a web site that helps visitors move from one page to another. A good website navigation system is imperative for a successful and accessible website design.
  • 7 Reasons You Should Avoid JavaScript Dynamic Navigation
    This informative article tells you 7 Reasons You Should Avoid JavaScript Dynamic Navigation. The author explains the technical reasons behind each of the 7, and provides excellent examples.
  • Top Website Navigation Tips
    The aim of a websites navigation is simply to allow users to get to the content they require. For sites that have a large number of sections and web pages (and information sites can be one of these) the navigation plan has to be properly researched and designed.
  • Tips for Improving Website Navigation
    A business often has more than one target audience it wants to reach with its website. Different customers mean different sales appeals and a homepage needs to be able to direct visitor traffic to go to the right places to get information or make a sale.
  • How To Easily Create A Search-Engine Friendly Navigation Menu For Your Website
    A navigation menu is an important element of a website. A good navigation menu helps your visitors navigate through your website efficiently - in as few mouse-clicks as possible.
  • Javascript Table Rollovers
    How you can get a table effect on rollover.
  • Skip Navigation Links are Important
    Providing links that allow the user to skip directly to content, bypassing the navigation, enhances the accessibility of your web site. This is recommended for blind or visually impaired users, people who use screen readers, and also for text-browsers, mobile phones and PDAs...
  • Ideal Navigation (Exclusive Article)
    What kind of navigation should a website have? Is it possible to make it ideal?
  • Visual Hierarchy
    Hierarchy. Creating website navigation, dear friends, is all about establishing hierarchy. This tutorial will teach you how it

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.