Location: PHPKode > scripts > Accessible nested DHTML menus > accessible-nested-dhtml-menus/FSMenu.php
File system menu class - an example of how to use the menu.  
This class dynamically builds a menu using the MenuNode class.  It's an example of
how the menu might be built dynamically from any kind of data.

Author: Mick Sear
eCreate, July 2005
License: LGPL - This means you can use it in your apps (commercial as well as 
non-commercial, but this message must be kept intact, source code must be provided to 
clients, and changes to the code must be marked as by yourselves.
 class FSMenu {
     var $base;
     var $menu;
     function FSMenu($basePath){    
         $this->base = $basePath;
         if (is_dir($this->base)){
            $this->menu = &new MenuNode();
            $this->recurseDir($this->base, $this->menu);
         } else {
            echo "Couldn't open directory ".$this->base;    
     //The parent menu Node is passed in by reference so we can add nodes at an arbitrary depth
     function recurseDir($path, &$menuNode){
         $d = opendir($path);
         while (false !== ($entry = readdir($d))) {
             if($entry != "." && $entry != ".."){
                 if (is_dir($path."/".$entry)){
                     $a = &new MenuNode();                 
                     //Current entry is set automatically in MenuNode by adding a class attribute of 'current'.  
                     //Current menu is carried over in URL
                     $dirForUrl = substr($path."/".$entry, strlen($this->base));
                     $menuNode->addNode($a, $entry, $_SERVER['PHP_SELF']."?dir=".$dirForUrl);
                     //This level of menu is passed to the recursive call by reference so recursive
                     //calls build on the current level of menu.  Allows for arbitrary depth of nesting
                     $this->recurseDir($path."/".$entry, $a);
                 } else {
                     //If you want to display files in the menu, you can do so here.  
                     //$this->elements[]['item'] = array('title'=>$entry, 'url'=>$path."/".$entry);
     function display(){         
         return $this->menu->display();
     This javascript needs to go in the document head, so there's a separate method to return it here.
     function getJavascript(){
         return $this->menu->getJavascript();
Return current item: Accessible nested DHTML menus