PHP 5 / Breadcrumbs navigation with SimpleXML, XPath and class

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 below). 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.

The scripts snippets you see here (the complete script is downloadable) 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). Best regards Claudio Biesele

PHP code in HTML file:

require_once '../class/xml.breadcrumb.class.php';
$b = new XMLBreadcrumb();
$b->PrepareBreadcrumb($url, $txt, $navid, $path, $matching1, $matching2, $pmain);

Part of Class code:

if ($res = $xml->xpath($pathmain))
    echo "<table width='200' border='1' cellspacing='2' cellpadding='0'><tr>
    <td nowrap><a href=$url>$txt <<</a></td>";
    for ($x=0; $x < $id; $x++)
    foreach($xml->link[$x]->attributes() as $a => $b)
        if($b != $id)
            $uri = $res[$x]->uritext;
            $url = $res[$x]->urltext;
            echo "<td nowrap>&nbsp;->&nbsp;<a href=$url?pageid=$b>$uri</a></td>";
            $uri = $res[$x]->uritext;
            echo "<td nowrap>&nbsp;>>&nbsp;<i>$uri</i></td>";
echo "</tr></table><br/>";

Part of XML code:

<?xml version="1.0" ?>

  <link id="1">
    <uritext>First Page</uritext>
  <link id="2">
    <uritext>Second Page</uritext>

Picture breadcrumb_1.jpg
1. Step: XML breadcrumbs menu in starting mode.
Picture breadcrumb_2.jpg
2. Step: XML breadcrumbs menu in opening mode - showing first page.
Picture breadcrumb_3.jpg
3. Step: XML breadcrumbs menu in opening mode - showing third page.
Download the full script at My part of the downloaded script is free from Copyright restrictions, Zurich, 16th of November 2015.
Donate with PayPal: