Advertisement
LanceCaraccioli

Nested Nav Data - Recursive Rendering

Jan 31st, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2.  
  3. $items = array(
  4.     '1' => array('text'=>'Home','title'=>'Return Home','uri'=>'/home'),
  5.     '2' => array('text'=>'About Us','title'=>'About Our Company','uri'=>'/about'),
  6.     '3' => array('text'=>'Learn More','title'=>'More Information','uri'=>'/more-info','children'=>
  7.         array(
  8.             '5' => array('text'=>'History','title'=>'Company History','uri'=>'/history'),
  9.             '6' => array('text'=>'Products','title'=>'Company Products','uri'=>'/products'),
  10.             '7' => array('text'=>'Services','title'=>'Company Services','uri'=>'/services'),
  11.         ),
  12.     ),
  13.     '4' => array('text'=>'Contact Us','title'=>'Contact Our Company','uri'=>'/contact'),
  14. );
  15.  
  16. function renderNestedList($children) {
  17.     $html='';
  18.     if (count($children)){
  19.         $html.='<ul>';
  20.         foreach($children as $child){
  21.             list($text, $title, $href, $subChildren) = array_values($child);
  22.             //var_dump($child);var_dump(array($text, $title, $href, $subChildren));die();
  23.             $html .= "<li><a title=\"{$title}\" href=\"{$href}\">$text</a></li>";
  24.             $html .= renderNestedList($subChildren);
  25.         }
  26.         $html.='</ul>';
  27.     }
  28.     return $html;
  29. }
  30.  
  31. echo renderNestedList($items);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement