PHP – Dynamic Navigation Class – Stop losing time editing navigation!

By | February 15, 2008

I often have to deal with client that want to made some ‘small changes’ in the navigation menu. Put element 2 before element 1 and change the name or the link…

So lazy as I’m, I decided to write (my first) a PHP Class, that should handle a two level navigation. Let me share with you how to use it:

< ?php
	include_once("Navigation.php");//the Class
	include_once('navigation_definition.php');// Array of Nav. Element
	$myNav = new Navigation($a); //Construct Nav.
?>

As you see we need to define a navigation file. This file is an associative array giving a list of information:

  • “key”: link name
  • “class”: the css class name
  • “href”: the URL of the link
  • “parent”: the key of the parent element
< ?php
$a = array(
"Top Level 1"=>array("class"=>"level1", "href"=>"test1.php", "parent"=>"no"),
"Top Level 2"=>array("class"=>"level1", "href"=>"test2.php", "parent"=>"no"),
"Level 2 sub 1"=>array("class"=>"level2", "href"=>"../nav/test3.php", "parent"=>"Top Level 2"),
"Level 2 sub 2"=>array("class"=>"level2", "href"=>"../nav/test4.php", "parent"=>"Top Level 2"),
"Level 2 sub 3"=>array("class"=>"level2", "href"=>"../nav/test5.php", "parent"=>"Top Level 2"),
"Top Level 3"=>array("class"=>"level1", "href"=>"test6.php", "parent"=>"no"),
"Level 3 sub 1"=>array("class"=>"level2", "href"=>"../nav/test7.php", "parent"=>"Top Level 3"),
"Level 3 sub 2"=>array("class"=>"level2", "href"=>"../nav/test8.php", "parent"=>"Top Level 3"),
"Level 3 sub 3"=>array("class"=>"level2", "href"=>"../nav/test9.php", "parent"=>"Top Level 3")
);

?>

Now it’s done, you can see a working example here.
If you like it, download the zip containing the Class, a navigation definition and a CSS file.

This will only work on PHP5 by the way…

Ahmet

 
0 Kudos
Don't
move!

2 thoughts on “PHP – Dynamic Navigation Class – Stop losing time editing navigation!

  1. not working

    This is not workind with php 5 because of split function (must be preg_split)

    Reply

Thoughts?