Home > PHP >
Create a Hierarchical Navigation Menu | Sitemap Search |
|
Sections Membership Features
Recent comments
very difficult by alfin Taking the credit for another persons work ? by curious dude. |
Create a Hierarchical Navigation MenuPosted by martin on 1 Jun 2002, last updated on 25 Aug 2002. How to create a hierarchical navigation menu which expends the current section. Why would one want hierarchical navigation links ah? — well this really improves organization as a whole, eases finding of information and access to related information. What's requiredFirst of all your site will have to use a hierarchical structure or have some special way of representing it, using canonical URIs is recommended, you won't get the result wanted otherwise. My site uses uses some remapping techniques so that more friendly URIs are presented to the user (and robots of course). If you want to know more about that a good start is to read Search Engine Friendly URLs, URLs! URLs! URLs! or How to Succeed with URLs. Apache's documentation also has many examples of the use of mod_rewrite. I'm using PHP for the code but there's no problem to use any given language that's used embedded in HTML or as a CGI, just some little adjustments need to be done. Configuration
A good starting point is adding some configuration to your DirectoryIndex site index.php <Files "site"> ForceType application/x-httpd-php </Files>
Action handle_my_php_pages /site AddHandler handle_my_php_pages .php These two lines can ensure that your global page handler will also be called when a direct request for a php file is made. This is optional and may require some modifications to the code. Style rules to use
ul.links {
margin-left : 0;
padding-left : 0;
}
ul.links li {
margin-left : 0;
list-style : none;
}
ul.links li ul {
padding-left : 2em;
margin-left : 0;
}
This will prevent the default rendering of the unordered list items that browsers do and change it into something that's more likely to look appropriate for navigation links. The core of it
I have all PHP code that is shown below saved in a file named $pages = array( 'Home' => 'home', 'News' => array( 0 => 'news/', 'Europe' => 'news/europe', 'USA' => 'news/usa', 'Asia' => 'news/asia'), 'Contact' => 'contact'); $root = 'shaggy'; $handler = 'site';
The The other two variables are set to the root of your site, and to the page that will handle file includes.
$params = explode('/', $HTTP_SERVER_VARS['REQUEST_URI']);
array_shift($params);
foreach ( $params as $i => $value ) {
if ( $value == $root or $value == $handler )
array_shift($params);
}
$include_file = implode('/', $params);
if ( sizeof($params) < 2 ) {
$include_file = 'root/' . $include_file;
$params[0] = 'root';
}
$include_file = explode('?', $include_file);
$include_file = $include_file[0];
if ( $params[sizeof($params)-1] == '' ) {
$include_file .= 'index';
}
$include_file .= '.php';
if ( ! file_exists($include_file) ) {
$include_file = "$params[0]/index.php";
}
include($include_file);
First explode the URI on
Directory indexed end with "/"(a slash) so append
Finally, perform a check if the file exists, if not forward to the section index (this code may need some improvements if you think this is likely to happen often).
The
function create_nav_links( $section, $level ) {
global $params;
if ( ! $level ) {
$markup = '<ul class="links">' . "\n";
} else {
$markup = "<ul>\n";
}
foreach ( $section as $desc => $filename ) {
if ( ! is_array($filename) ) {
if ( $desc )
$markup .= '<li><a href="' . $handler . '/' .
$filename . '">' . $desc . "</a></li>\n";
} else {
if ( $params[$level] . '/' == $filename[0] ) {
$markup .= '<li><a href="' . $handler . '/' .
$filename[0] . '">' . $desc . "</a>\n";
$markup .= create_nav_links($filename, $level +1);
$markup .= "</li>\n";
} else {
$markup .= '<li><a href="' . $handler . '/' .
$filename[0] . '">' . $desc . "</a></li>\n";
}
}
}
return ( $markup . "</ul>\n" );
}
echo create_nav_links($pages, 0);
First check if we are at level 0, the topmost, if so add a class of
The man loop traverses the array passed as a parameter, first we check to see if we're
dealing with a normal file — i.e. not a section (which is represented as an array in our
In the other case — a category check (
If this is some other category, the visitor is probably not interested in, just display
a links to its index —
Finally
A little something that has to be added to the <?php $base = 'http://' . $HTTP_SERVER_VARS['SERVER_NAME'] . $HTTP_SERVER_VARS['SCRIPT_NAME']; ?> <base href="<?php echo $base?>" />
That ensures the consistency of your links generated by the What elseYou noticed that last note about the links, right? Well, it ensures that these links are okay but when you begin using anchors in your documents you can't use anchor names relative to the current page, you'll have to make them relative to your document root. You smarty, why don't you fix your function — well it will be not a problem to fix this function but what about any global style sheets or images referenced from your pages — you'll have to modify them for every page.
A better solution to this problem would be to use a function for creating links that are absolute (for the site) and get rid of the This way to hold the navigation menu, as an PHP array, is good only if you want to update it into the source everytime you change something. A better solution would be to use a database to hold that data, even you may want to get the contents of the pages from a database source. A version of the code with a database backend is available for download. CommentsLive demo? by rachel cunliffe (rachel@cre8d-design.com) on 10 Jun 2002 9:19pm GMT Hiya do you have a live demo -- would be nice to see the code working :) A screenshot only by martin on 8 Jul 2002 11:51pm GMT The last thing that still remains from the hierarchical navbar is a screenshot, I don't think now that such navigation can help people because it implies shorter titles which are generally harder to understand. Ugly URLs by Andrew (adyus@yahoo.com) on 17 Dec 2003 1:41pm GMT How would the script work or what would I need to modify to use this script with 'ugly URLs'? I think I'll have to rewrite it from the bottom up. Anyway, your tutorial is great! Waste of time by Yankee (yankee@maintorrent.com) on 15 Jun 2004 8:26am GMT this scripts doen not work. How do you do this? by ArcaneErudite (stealth_is_@hotmail.com) on 6 Jul 2004 12:25pm GMT I just read your article about using JavaScript and PHP to adjust the time shown. How would you use JavaScript to tell what div tag you are hovering over, and pull up a custom right click menu by submitting it to php or a database. I didn't know you could get JavaScript and PHP to talk. Do you think it would be possible to do this? |