SEARCH
Newsletter
Subscribe to get design tips, latest trends, free stuff and more.
It doesn't look like an e-mail address

hosting

  Web Programming PHP Php navigation

Php navigation

Alex PHP Jan 29, 2007

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="/img_articles/10746/news.htm"> should become
<a href="/img_articles/10746/?id=default">

<a href="/img_articles/10746/whatever2.htm"> should become
<a href="/img_articles/10746/?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="/img_articles/10746/?id=default">News</a>
<a href="/img_articles/10746/?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

subscribe to newsletter