Location: PHPKode > scripts > XML Site Navigation > xml-site-navigation/menu_class.php
<?php
/**
 * Menu includes two methods for first loading an XML tree and then parsing
 * that tree to build level-appropriate menus.
 *
 * The load_menu method takes an XML tree and loads it into the 'xml' property
 * by way of the simplexml_load_file fuction.
 *
 * The write_menu method takes a level, passed as an integer, to write out the
 * user-level-appropriate menu.
 *
 * The menu items can be fully customizable through CSS properties to be either
 * horizontal or vertical, with each item being its own DIV, as illustrated below.
 *
 * Example usage:
 *
 * require('menu_class.php'); // If you're using the 'classloader.php' module you could omit this line.
 * $menu = new menu();
 * $menu->load_menu('xml/menu.xml'); // Change this to your own prefered XML file path.
 * $menu->write_menu(1)
 *
 * This same methodology could be used to manage multiple menus in a single XML menu tree,
 * based not on access-level, but on a per-page structure with each page having its own
 * menu items with a page identifier as an element instead of level.
 *
 * Copyright 2009 Geoff Foley
 * Distributed under the terms of the GNU General Public License
 * http://www.gnu.org/licenses/gpl-3.0.txt
 */
class menu {
    public $xml;

    public function load_menu($xml_file) {
        $this->xml = simplexml_load_file($xml_file);
    }

    public function write_menu($level) {
        $level = "level{$level}";
        $m_cnt = count($this->xml->menu->$level->item);
        $menuItemDisp = "\n";
        for($i = 0; $i < $m_cnt; $i++) {
            $target = '';
            if($this->xml->menu->$level->item[$i]->target == 'new')
                $target = ' target="new"';

            $menuItemDisp .= "<div class=\"menuItem\"><a href=\"".$this->xml->menu->$level->item[$i]->url."\"{$target}>".$this->xml->menu->$level->item[$i]->title."</a></div>\n";
        }
        $menuItemDisp = substr($menuItemDisp, 0, -1);
        return $menuItemDisp;
    }
}
?>
Return current item: XML Site Navigation