IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Creating Common Navigation For A Website: Part 2

, Newbies guide to creating common navigation with PHP


pasten
no avatar
Advanced Member
*******
Group: [HOSTED]
Posts: 119
Joined: 19-September 06
From: In front of my monitor.
Member No.: 30,215
myCENT:34.77



Post #1 post Oct 27 2008, 07:15 AM
Prerequisite: Basics of HTML.

Download example Attached File  Creating_common_navigation_for_a_website___Part_2.zip ( 3.41K ) Number of downloads: 1
(.zip) (needs a php enabled server to view the result of the scripts)

The first part of the tutorial (click here) described the method of creating common navigation for websites using pure html. That was posted almost a year ago. I happened to visit that, and thought to write a second part for it. As the previous article this article too is for absolute newbies guiding them to create a common set of links for a website. But this method uses PHP scripting. If you don't know what is PHP, for time being just know that the method described ahead will work on any free/paid web host offering php. Also don't worry about the php code, just read a basic php tutorial and you will be on the track. If you are php developer then you may also read the latter part of the article and leave your comments about it.

Ok, so let's get started.

1) Firstly, create an html page containing all the links which you want to appear on every page of your website. Name this file as "nav.html". The html code should look something like this:

CODE
<p>
<a href="page1.php">Page 1</a> | <a href="page2.php">Page 2</a> | <a href="page3.php">Page 3</a>
</p>


You would better link those files as site relative (for example http://www.domain.tld/art/article.html). This will enable you to create any number of subdirectory combinations.

2) Now create the pages "page1.php", "page2.php" and "page3.php". But... wait, if you are using windows there is a problem in creating those files. So, first select the "Tools" menu, then select "Folder Options...". In the popup box select the "View" tab and uncheck "Hide extensions for known file types", press OK. Now right click anywhere and choose "New" "Text Document" to create the files.

3) In each of the pages, at the top of document, even before html tags/doctype you need to add the following code:

CODE
<?php
//universal include function for site wide includes
function incfile($file,$d=""){
for($i = 0; $i < 5; $i++){
  if(!is_file($d.$file)){
   $d.="../";
  }else{
   include ($d.$file);
   break;
  }
}
}
?>


The code contains a simple php function which, when processed adds "../" after each loop until the path is correct. In simple, this function allows you to make upto 5 (change the number if you want more) folders and create a document just containing the code given in step 4. You don't have to worry about the paths at all.

4) Now place this code in each of the page wherever you want to show the link:

CODE
<?php incfile("include/nav.html"); ?>


"include" is the top level folder containing the nav file. You can also have the file in server root, by making appropriate changes to the path.

Hey! That's it. You've done. Now if you did till here you know what to do next. Put the files on to the server and open page1.php from the browser. Now you have a common set of links which you can change once in "nav.html" and it is updated for the whole website (may be containing 100's of pages).

Advantages:
1) A simple, manageable set of links which you can change anytime only at one place and the whole site is updated.
2) Unlike the iframe method described in first part, this method doesn't add any extra code to the html.
3) Unlike in the iframe method, here, in this method nobody visiting your website can know that you are using a common set of links for every page.

Disadvantages:
1) Nothing in general, but just don't go overboard, "include"ing files everywhere on the page. Because each time you inlcude a file, the server has to open the file, read it, transfer it and then close it. So, the website may take a slight performance hit, not noticeable to living beings laugh.gif.
2) This is a solution for very basic websites. You will need CMS (Content Management System) if you want complex, dynamic, interactive websites. Even CMS's use "include"/"require", but that's another matter.

Some technical notes for the PHP developers:

You may ask "Why can't we use simple php include/require?". Wait... this tutorial is aimed at newbies who don't know much about server side scripting. So naturally they are creating individual pages instead of a PHP-MySQL templating system or similar methods. So what exactly happens is that, when we use include we have to change the path of the include file depending on the directory the current file resides. So the idea was to abolish the usage of default include funtion and automate the path defining process. That is what the function does.

As in include/require, there's no need to worry about directory relative/root relative/document relative paths. fsockopen includes are also not a good solution, as they are not enabled by default on most web hosts.

I came across that function in one of the php developer forums. One of my friends there told me about it. Actually I don't know who is the original creator of the function. Anyway that is really a creative idea. All the credit goes to that anonymous person. I have just presented you a practical example so that you can learn it and apply those in your projects. Happy coding.

Any doubts, comments are welcome.

Edit: Corrected malformed codes.

This post has been edited by pasten: Oct 28 2008, 06:02 AM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts 12 noxit 11,479 26th February 2009 - 12:59 AM
Last post by: novic_1223
No new   60 -iancovenant- 6,569 13th November 2009 - 11:45 AM
Last post by: templehost
No new   27 Dagoth Nereviar 4,304 22nd August 2009 - 09:38 PM
Last post by: Nameless_
No new   14 rpgsearcherz 1,902 3rd September 2009 - 05:01 PM
Last post by: webishqiptar
No New Posts   5 Thunder 8,209 16th April 2009 - 08:21 AM
Last post by: amrik09
No new   17 EricDrinkard 16,928 11th August 2009 - 01:13 PM
Last post by: fadillzzz
No new   61 Etherion 45,060 17th November 2009 - 06:14 AM
Last post by: Johnmck93
No New Posts   4 andyy15 6,351 27th May 2009 - 09:36 PM
Last post by: Dook-E
No New Posts   4 FreedomOverdose 630 22nd August 2009 - 09:30 AM
Last post by: ASHISHRANJAN
No New Posts   1 Jarek_Gd 962 24th February 2008 - 12:49 PM
Last post by: rvalkass
No new   23 PnOyKiO 21,570 20th October 2009 - 12:22 PM
Last post by: -Sky-
No new   45 Dehornos 7,227 7th November 2009 - 08:01 PM
Last post by: bigtimedrama
No new   16 sonesay 2,594 31st July 2009 - 06:27 AM
Last post by: iworld200
No new 18 Vacant 16,260 26th September 2004 - 10:13 AM
Last post by: gotcha41
No new   33 djleli 13,762 17th September 2006 - 08:14 AM
Last post by: jlhaslip


 



RSS Open Discussion Time is now: 22nd November 2009 - 02:36 AM

Web Hosting Powered by ComputingHost.com.