You are here:  » multi level navigation


multi level navigation

Submitted by ninjabonsai on Thu, 2010-04-01 09:41 in

hi.. i'm trying to build a multi level navigation with MagicParser from this xml file
http://www.christinepichler.com/nav.xml

the following will print out top level items but i'd like to show a complete structure with sub items too

function buildNav($record)
{
    $folder = $record["NAVITEM-FOLDER"];
    echo '<a href="http://www.christinepichler.com/index.php?page='.$folder.'">'.$folder.'</a><br />';
}
MagicParser_parse("nav.xml", "buildNav", "xml|NAV/NAVITEM/");

any suggestions?
cheers

Submitted by support on Thu, 2010-04-01 10:12

Hi,

I just saw your email but i'll reply here as the same technique may help another user in the future. In this instance, as the XML is hierarchical to multiple levels (and is also small), it would be best to read the file at the top level (using Format String "xml|NAV/").

This will give you a complete list of navitems (which can be seen here), where the depth can be determined from the number of instances of NAVITEM in each field.

You can then use foreach() to process every item in the list, looking for HEADING and FOLDER keys, and when found, set $currentHeading and $currentFolder value. When you have both; count the number NAVITEM occurrences in $key, and create <ul> or </ul> tags to create a hierarchical display of your navigation. Finally, create the link within <li> tags.

Have a go with this code:

<?php
  
require("MagicParser.php");
  function 
buildNav($record)
  {
    global 
$depth;
    
$currentHeading "";
    
$currentFolder "";
    foreach(
$record as $key => $value)
    {
      if (
strpos($key,"HEADING")!==FALSE)
      {
        
$currentHeading $value;
      }
      if (
strpos($key,"FOLDER")!==FALSE)
      {
        
$currentFolder $value;
      }
      if (
$currentHeading && $currentFolder)
      {
        
$newDepth substr_count($key,"NAVITEM");
        if (
$newDepth $depth)
        {
          while(
$depth $newDepth) { print "<ul>"$depth++; }
          
$depth $newDepth;
        }
        elseif (
$newDepth $depth)
        {
          while(
$depth $newDepth) { print "</ul>"$depth--; }
          
$depth $newDepth;
        }
        print 
"<li><a href='http://www.christinepichler.com/index.php?page=".$currentFolder."'>".$currentHeading."</a></li>";
        
$currentHeading "";
        
$currentFolder "";
      }
    }
  }
  
$depth 1;
  print 
"<ul>";
  
MagicParser_parse("nav.xml""buildNav""xml|NAV/");
  print 
"</ul>";
?>

(output running on this server here)

Hope this helps!
Cheers,
David.

Submitted by ninjabonsai on Thu, 2010-04-01 11:41

wow.. thanks a lot david!