Stilistic Dev /// Munky Style head_bg head end
nav begin hometutorialsgallerydownloadsservicesportfoliocontactabout
default   blue   orange   black   purple   yellow   teal   green   red  


Google
 
Web StilisticDev.net
mhtr PHP Linksmhtr
 
mbtl   mbtr
  Ever wonder how to make navigation where you stay on the index page, but the content changes? I'm sure you have seen the URL of pages like this, they often look similar to this: index.php?content=Home.



This is actually very simple. It is done using the $_GET variable, and includes.
Lets start off with the Index.php page.

<?
$content = $_REQUEST['content'];
if
($content == "") {
$content = "home";
}
echo<<<EOF

<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<
head>
<
title>Your Title</title>
</
head>

<
body>
EOF;
include
"$content.inc";

echo<<<
EOF

<
a href="index.php?content=home">Home</a>
<
a href="index.php?content=links">Links</a>

</
body>
</
html>
EOF;
?>




Now for the breakdown. First we have:

$content = $_REQUEST['content'];




Pretty much what this does is pulls the content from the URL and sets the $content variable to that. For example if we had a URL that look like this: index.php?content=gallery, the $content variable would be set to gallery.



next we have:
if($content == "") {
$content = "home";
}




This says if the $content variable is not set, to set it to home. This is very important if you are working with the index file. Most servers read index.(some extenstion) as the main page, therefor when you first visit the page, no variable will be sent, which results in an error message.



The next section is all html code, which I wont explain, there may be something you're not used to seeing in here though. instead of using echo, im using echo<<<EOF.

this says to echo everything until EOF. You'll then see EOF; . This is what stops the echo. By doing this, you can use quotes in your html code, without having to escape them.
One very important thing. You CAN NOT have anything after the echo<<<EOF until the next line of code. ie, you cant have echo<<<EOF then a space, same goes with the EOF;. If you have anything after these, even a simple space, it will break the code and return an error. Also, from personal experience, if you use dreamweave, beware. Dreamweave r sometimes places hidden characters in the code, which cant be seen in dreamweaver, but can be seen from the server, therefor breaking the code. Ok enough about that, now to the next part of the code

include"$content.inc";




This will include our content. Include lets us include a seperate file in our documents. In this case the file included will depend on the $content variable. For example, if the link index.php?content=home is clicked, home.inc will be included. We will discuss the include files shortly.

now the last part

<a href="index.php?content=home">Home</a>
<
a href="index.php?content=links">Links</a>




These are our links. The ?content= is what will set the $content variable. So, index.php?content=home would set the $content variable to home, which would include home.inc. Content= should be the same name as your include file. For example, if you have the include file portfolio.inc the link would look like this: index.php?content=portfolio.



Now for the include file. An include file is simply a file that will be included in our docuemnt. An easy way to think of the include is to think of copy and paste. The document will copy anything inside the include file, and paste it where we say to include it. This means you can have any type of code in the include file. So lets make two new documents, home.inc and links.inc. Now lets enter some code.
for home.inc we'll enter:


<p>This is the home page</p>




Now for the links.inc



<p>This is the links page</p>



Well that's it, we now have PHP links.





 
mbbl   mbbr