Location: PHPKode > scripts > Breadcrumbs navigation with PHP 5, SimpleXML, XPath and class > breadcrumbs-navigation-with-php-5-simplexml-xpath-and-class/php_publ310806.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 5 / Breadcrumbs navigation with SimpleXML, XPath and class</title>
</head>

<body>

<table border="0" width="100%">
  <tr>
    <td width="100%"><font face="Arial" size="1">Navigate through a website can be most irritating if the user is not well informed where he is and where he comes from. To prevent users from being lost the breadcrumbs method is one of the most suitable - imagine you drop breadcrumbs in a forest for not loosing orientation. SimpleXML, XPath and PHP 5 are perfect tools to create serious breadcrumbs positioning. This class is taking care of the breadcrumbs handling (see screenshots <a href="#screenshot">below</a>). The XML file contains all the necessary data to control standard and breadcrumbs navigation (see XML code below). The script controls seven parameters you deliver according to your environment and XML file by adapting the PHP 5 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 breadcrumbs elements. In the second part we use only a different technique to present the XML data (thanks to HTML table): First we read the id and set the corresponding elements (if id is 2 we show only the homepage, plus the first and the second link) - besides that we disable the link of the last element shown in the breadcrumbs navigation.<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 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>PHP code in HTML file:</b></font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">require_once '../class/xml.breadcrumb.class.php';<br></font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">$b = new XMLBreadcrumb();<br></font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">$b-&gt;PrepareBreadcrumb($url, $txt, $navid, $path, $matching1, $matching2, $pmain);</font></p>
					<p><font face="Arial" size="1"><b>Part of Class code:</b></font></p>
					<p><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">if(isset($_GET[$navid]))<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> {  <br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">  if ($res = $xml-&gt;xpath($pathmain))<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">  {<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   &nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;table width='200' border='1' cellspacing='2' cellpadding='0'&gt;&lt;tr&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   &nbsp;&nbsp;&nbsp;&nbsp;&lt;td nowrap&gt;&lt;a href=$url&gt;$txt &lt;&lt;&lt;/a&gt;&lt;/td&gt;&quot;;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   &nbsp;&nbsp;&nbsp;&nbsp;for ($x=0; $x &lt; $id; $x++)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   &nbsp;&nbsp;&nbsp;&nbsp;{    <br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">    &nbsp;&nbsp;&nbsp;&nbsp;foreach($xml-&gt;link[$x]-&gt;attributes() as $a =&gt; $b)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">    &nbsp;&nbsp;&nbsp;&nbsp;{<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($b != $id)<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uri = $res[$x]-&gt;uritext;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$url = $res[$x]-&gt;urltext;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;td nowrap&gt;&amp;nbsp;-&gt;&amp;nbsp;&lt;a href=$url?pageid=$b&gt;$uri&lt;/a&gt;&lt;/td&gt;&quot;;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">     
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uri = $res[$x]-&gt;uritext;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;td nowrap&gt;&amp;nbsp;&gt;&gt;&amp;nbsp;&lt;i&gt;$uri&lt;/i&gt;&lt;/td&gt;&quot;;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">    &nbsp;&nbsp;&nbsp;&nbsp;}<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   }<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">   echo &quot;&lt;/tr&gt;&lt;/table&gt;&lt;br/&gt;&quot;;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">  }</font></p>
					<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>
							&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;&lt;uritext&gt;First Page&lt;/uritext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&lt;urltext&gt;http://localhost/Breadcrumbs/first_page.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;link id=&quot;2&quot;&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&lt;uritext&gt;Second Page&lt;/uritext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&lt;urltext&gt;http://localhost/Breadcrumbs/second_page.php&lt;/urltext&gt;<br>
						</font><font size="1" color="#0066cc" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&lt;/link&gt;<br>
							&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="breadcrumb_1.jpg" width="460" height="155"></td>
  </tr>
  <tr>
    <td width="100%"><font face="Arial" size="1"><b>1. Step:</b> XML breadcrumbs menu in starting mode.</font></td>
  </tr>
  <tr>
    <td width="100%"><img border="0" src="breadcrumb_2.jpg" width="460" height="155"></td>
  </tr>
			<tr>
    <td width="100%"><font face="Arial" size="1"><b>2. Step:</b> XML breadcrumbs menu in opening mode - showing first page.</font></td>
  </tr>
			<tr>
				<td width="100%"><img src="breadcrumb_3.jpg" alt="" height="155" width="460" border="0"></td>
			</tr>
			<tr>
				<td width="100%"><font face="Arial" size="1"><b>3. Step:</b> XML breadcrumbs menu in opening mode - showing third page.</font></td>
			</tr>
			<tr>
    <td width="100%"><font face="Arial" size="1">Download the full script at <a href="http://www.fastproject.ch/publikationen/xml_breadcrumbs.zip#load" target="_blank">www.fastproject.ch</a>. My part of the downloaded script is free from Copyright restrictions, Zurich, 31th of August 2006.<a name="load"></a></font></td>
  </tr>
		</table>

</body>

</html>
Return current item: Breadcrumbs navigation with PHP 5, SimpleXML, XPath and class