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.
Related Articles
- The Web Adhesive, Reciprocal LinksIn web related works the term Reciprocal links have a significant importance. Their role cannot be eliminated and thus their knowledge is also of equal importance. Basically, Reciprocal links form an imperative ingredient of any website promotion endeavor...
- Building Quality Links to Your Web SiteIf you want to boost your search engine rankings, search engine optimization is one technique you can use to drive targeted traffic to your site and turn your clicks into cash.
- Information Regarding BacklinksYet another topic that requires necessary revelation is the area of back links. To be very precise the definition goes as follows, Back links are to incoming links to a website. For example, a site with a lot of back links implies that many other sites link to that site...
- Search Engine Optimization (SEO) - Fix Your OffPage!Search Engine Optimization (SEO) is something you should be aware of before creating a site. Make sure youve done careful researches on the best keywords to use. Using the wrong keywords would eliminate your site from search engines forever!...
- How To Get Backlinks and Increse Traffic To Your Site/ BlogsGetting back links are the one of the important way to increase the page rank of the sites. Because most search engines calculating their page rank according to the no of back links and the percentage of keyword density present in the WebPages.
- How to Find Good LinksThis is where it gets tricky. The first thing you need to do is find a number of sites on a similar theme to yours. You can do this through the search engines, or from other peoples links lists. Try to build up a small list of sites that you want to tra...
- Should you Buy Links to your Website?You probably already know that links are one of the most powerful ways to get your site ranked for your keywords. Other people know this, too, and so many websites have popped up that are selling links, either links from their own site(s) or links from other sites.
- How To Optimize Your New Site The Correct WayIf you are going to start search engine optimization on your website you can follow these methods to start and do SEO the right way. These method will help you get ranked top in Google.
- 9 Near Fatal Flaws Most Links Pages Suffer From And How To Avoid ThemTrading links is critical to online success. Yet most links pages are defective! The worst ones can have nine near fatal flaws. How does yours stack up?
- Re-Use the Links You Have When Re-Vising Your Site!Many times Ive seen a web site undergo a revision and everything is brand new, even brand new links! Youve worked so hard to get those great page ranks for all your pages and then you get rid of them? NO NO NO...
