Section Menu v1.2

This is the “topbar” block available in the dummy content of the latest release. It contains links to all sections, placed in a hierarchy list:

<?php
 
$items = $db->select( '*', $config['Sections'], false, 'id');
if( is_array($items) ){
  $children = array();
  foreach( $items as $k => $v ){
    preg_match('/(.*)\((.*)\)/', $v['position'], $matches);
 
    if($matches[2] == 'x') {
      array_push($children[$matches[1]], $v['id']);
    } else {
      $children[$matches[1]][$matches[2]] = $v['id'];
    }
  }
  $output = createOrder( $items, $children );
}
echo '<div id="topbar">';
echo $output;
echo '</div>';
 
function createOrder( $items, $order, $parent=0, $path='' ){
 
  ksort($order[$parent]); 
 
  $listings .= '<ul>';
 
  $middle = '</span></a>';
  $close = '</li>';
  foreach( $order[$parent] as $k => $v ){
    foreach( $items as $l => $w ){
      // now we can create the listings
      if( $w['id'] == $v ){
        $open = '<li><a href="/'. $path . $w['slug'] .'.html"><span>';
        // check for children in this branch
        if (array_key_exists($w['id'], $order)) {
		  $new_path = $path . $w['slug'] . '/';
	      $children = createOrder( $items, $order, $w['id'], $new_path );
	    } else {
	      $children = '';
	    }
	  $listings .= $open . $w['title'] . $middle .  $children . $close;
      }
    }
  }
  $listings .= '</ul>';
 
  return $listings;
}
 
?>

Instructions:
- Copy/paste the following code and save it as a PHP block
- Edit the $open , $close variables if needed for a custom layout…

Change log:
- v1.2 : Better linking of children. Proper order restored.
- v1.1 : Updated the ‘parent’ field to the new ‘position’ field. Additional tweaks.
- v1.0 : Initial release

Leave a Reply