Location: PHPKode > scripts > Menu Handling Class with SimpleXML and PHP > menu-handling-class-with-simplexml-and-php/php_publ220806.htm
<html>

<head>
<meta http-equiv="Content-Language" content="de-ch">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>PHP / Menu Handling Class with SimpleXML and PHP 5</title>
</head>

<body>

<table border="0" width="100%">
  <tr>
    <td width="100%"><font face="Arial" size="1">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 <a href="#screenshot">below</a>). 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.<br>
					</font>
					<p><font face="Arial" size="1">T</font><font face="Arial" size="1">he scripts snippets you see here (the complete script is <a href="#load">downloadable</a>)&nbsp;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). <i>Best regards Claudio Biesele</i></font></p>
				</td>
  </tr>
			<tr>
    <td width="100%">
      <hr>
					<p><font face="Arial" size="1"><b>Part of PHP code:</b></font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">// Url for closing the menu<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">$url = &quot;http://yourhost/SimpleXml_menuClass.php&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   // Add explaining text to the Url<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $txt = &quot;Close Menu&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">// name the ID for the GET identifier (delivers the ID to find the xml menu subnodes)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $navid = 'pageid';<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   // Path to the xml file<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $path = &quot;xml/navi.xml&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   // name the path to the matching node for the main menu text (according to xml file)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $matching1 = &quot;//uritext&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   // name the path to the matching node for the main menu url (according to xml file)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $matching2 = &quot;//urltext&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   // name the path to the matching node for the second main menu (according to xml file)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $pmain = &quot;/links/link&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">// name the path to the matching attribute (ID) for the sub menu (according to xml file)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   $psub1 = &quot;/links/sublink&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">// name the matching attribute (ID) for the sub menu (according to xml file)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">$psub2 = &quot;subid&quot;;<br>
						</font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> require_once 'class/xml.menu.class.php';<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $m = new XMLNavigation();<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $m-&gt;PrepareMenu($url, $txt, $navid, $path, $matching1, $matching2, $psub1, $psub2, $pmain);
					<p></p>
				</td>
  </tr>
			<tr>
				<td width="100%">
					<hr>
				</td>
			</tr>
			<tr>
				<td width="100%"><font face="Arial" size="1"><b>Part of XML code:</b></font>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><b>&lt;?xml version=&quot;1.0&quot; ?&gt;<br>
							</b></font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&lt;links&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&lt;link id=&quot;1&quot;&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;uritext&gt;Go to Claudio's&lt;/uritext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;urltext&gt;http://localhost/XPathMenu_Xml.php&lt;/urltext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&lt;/link&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&lt;sublink subid=&quot;1&quot;&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;urisubtext&gt;Claudio's Web Design&lt;/urisubtext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;urlsubtext&gt;http://www.fastproject.ch/web.php&lt;/urlsubtext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&lt;/sublink&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&lt;/links&gt;</font></p>
					<p></p>
				</td>
			</tr>
			<tr>
    <td width="100%"><font size="1" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><a name="screenshot"></a></font></td>
  </tr>
			<tr>
    <td width="100%">
      <hr>
    </td>
  </tr>
  <tr>
    <td width="100%"><img border="0" src="xml_menu1.jpg" width="350" height="160"></td>
  </tr>
  <tr>
    <td width="100%"><font face="Arial" size="1"><b>1. Step:</b> XML menu in closing modus.</font></td>
  </tr>
  <tr>
    <td width="100%"><img border="0" src="xml_menu2.jpg" width="350" height="160"></td>
  </tr>
			<tr>
    <td width="100%"><font face="Arial" size="1"><b>2. Step:</b> XML menu in opening modus.</font></td>
  </tr>
			<tr>
    <td width="100%"><font face="Arial" size="1">Download the full script at <a href="http://www.fastproject.ch/publikationen/XPathMenu_Xml.zip#load" target="_blank">www.fastproject.ch</a>. My part of the downloaded script is free from Copyright restrictions, the rest will do under GNU </font><font face="Arial" size="1">General Public License Version 2, Zurich, 22th of August 2006.<a name="load"></a></font></td>
  </tr>
		</table>

</body>

</html>
Return current item: Menu Handling Class with SimpleXML and PHP