PHP / Menu Handling Class with SimpleXML and PHP 5

The big part of programming web applications is writing, writing and writing code - and that means to do good work you need a lot of time. Classes are very usefull to write less code and set free more creativity for other web challenges. This script helps you to create a menu for navigation with sub elements in no time (see screenshots below). It's a combination between the excellent SimpleXML Class and some of my ideas. My class is taking care of the menu handling. The XML file is easy to create: Main navigation and sub navigation differ only from the name of the node, the id of main and sub node must be the same to assign to each other (see XML code below). The menu class handles how and when to open and close the navigation elements. The script controls nine parameters you deliver according to your environment and XML file by adapting the PHP code in the HTML file (see PHP code). The script is made of two parts: In the first part the class loops through the XML file taking and counting all the values from the main nodes and sets text, Url and Id (which has to start with 1 and needs to be unique and incremental) to access the corresponding sub elements. In the second part (in opening condition) there are three parts: First we read the id and set the corresponding main elements (if id is 2 we show only 2 main elements), second we show all sub elements with the same id and third we take all main elements and subtract the already shown main elements to present the rest of the menu.

The scripts snippets you see here (the complete script is downloadable) is only a small part of the complete application. That's it. The easy extendable xml data file and other stuff is all prepared for your convenience (xml- and class-files included). Best regards Claudio Biesele


Part of PHP code:

// Url for closing the menu
$url = "http://yourhost/SimpleXml_menuClass.php";

// Add explaining text to the Url
$txt = "Close Menu";

// name the ID for the GET identifier (delivers the ID to find the xml menu subnodes)
$navid = 'pageid';

// Path to the xml file
$path = "xml/navi.xml";

// name the path to the matching node for the main menu text (according to xml file)
$matching1 = "//uritext";

// name the path to the matching node for the main menu url (according to xml file)
$matching2 = "//urltext";

// name the path to the matching node for the second main menu (according to xml file)
$pmain = "/links/link";

// name the path to the matching attribute (ID) for the sub menu (according to xml file)
$psub1 = "/links/sublink";

// name the matching attribute (ID) for the sub menu (according to xml file)
$psub2 = "subid";

require_once 'class/xml.menu.class.php';
$m = new XMLNavigation();
$m->PrepareMenu($url, $txt, $navid, $path, $matching1, $matching2, $psub1, $psub2, $pmain);


Part of XML code:

<?xml version="1.0" ?>

<links>
  <link id="1">
      <uritext>Go to Claudio's</uritext>
      <urltext>http://localhost/XPathMenu_Xml.php</urltext>
  </link>
  <sublink subid="1">
      <urisubtext>Claudio's Web Design</urisubtext>
      <urlsubtext>http://www.fastproject.ch/web.php</urlsubtext>
  </sublink>
</links>


Picture xml_menu1.jpg
1. Step: XML menu in closing modus.
Picture xml_menu2.jpg
2. Step: XML menu in opening modus.
Download the full script at www.fastproject.ch. My part of the downloaded script is free from Copyright restrictions, the rest will do under GNU General Public License Version 2, Zurich, 16th of November 2015.
Donate with PayPal:
placeholder