html / xhtml, css, php, mysql, javascript, asp, seo, web management, semantics, accessibility and more.
An explaination behind includes, what they are, what they are used for and how to use them in your site to help reduce the amount of work you do and also reduce server load.
In category php.
Posted by Joseph Skidmore on 06.09.05.
Last edited on 06.09.05.
Lets say you have a site with 100 pages, all with the same navigation on each one. If you were to want to add or change a link you would have to go through all the pages and change each one separately. Well with includes this process can be simplified.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title></title> </head> <body> <h3>Navigation</h3> <ul> <li>Object 1</li> <li>Object 2</li> <li>Object 3</li> <li>Object 4</li> <li>Object 5</li> </ul> Rest of page content here </body> </html>
Say all of your pages start off like this and the only thing that changes is the content underneath your navigation, well using includes we can put the navigation in a single page of its own which will be included on each page we tell it to.
Open notepad and save a new file called 'navigation.php'. Inside this file copy your navigation, which on this tutorial is:
<h3>Navigation</h3> <ul> <li>Object 1</li> <li>Object 2</li> <li>Object 3</li> <li>Object 4</li> <li>Object 5</li> </ul>
With this code in a separate file (put it in the same directory as the rest of your files) add this small code in place of your navigation on each of the pages you want the navigation to appear on:
<?php include ("navigation.php"); ?>
On all pages you put this code, they must all have the *.php extension, if you still have *.html this will not work. This code simply includes the navigation into where you want it. Like so...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title></title>
</head>
<body>
<?php include ("navigation.php"); ?>
Rest of page content here
</body>
</html>
When the page with this code runs, it will look through your code and include the navigation where you place the include making the output exactly the way we had it at the top of the page, if you want to edit a link all you do is change the code inside navigation.php and all navigations on each page will change because they are all getting the navigation from navigation.php.
This code comes in handy when you have many pages all with content that you will change regularly, such as navigations / links / blogs / news ect. You can include as many files as you want, includes will make the running of your site so much easier, it has made life much easier for me.
Page generated in 0.0012 seconds.