Re: Having trouble with a multidimensional array menu

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Daevid Vincent wrote:
I'm trying to build a multi-array menu (but open to using classes or
something if that makes this easier).
Does anyone have a solution already working?

I'm very close, but I can't seem to get my "directories" to work and I
end up with extraneous <ul> blocks.

here's what I need to re-create:


Try this function instead.

function multiArray2MenuTree( $menu, $indent = 0, $sub = false ) {
  if ( is_array($menu) && count($menu) ) {
    print(str_repeat("\t",$indent).'<ul class="navTree toggleClosed">');
    foreach ($menu as $key => $value) {
      //echo "<b>$key</b> = $value<br/>\n";
      if ( is_array($value) ) {
        print(str_repeat("\t",$indent+1).
		'<li id="'.$key.'" class="file ext_txt"><a href="'.
		$value['page'].'">'.$value['alt'].'</a>');
        multiArray2MenuTree($value, $indent+1, true);
        print(str_repeat("\t",$indent+1).'</li>');
      } elseif ($key == 'alt' && !$sub) {
        print(str_repeat("\t",$indent+1).'<li id="'.$key.
		'" class="directory collapsed"><a href="#"
		class="toggle">'.$value['alt'].'</a></li>');
      } else {
        print(str_repeat("\t",$indent+1).'<li id="'.$key.
		'" class="directory collapsed">I did not match either if
		condition, you need to figure out why...</li>');
      }
    }
    print(str_repeat("\t",$indent).'</ul>');
  }
}

This might point out what is happening.

Now, to fix it.  Use this...

function multiArray2MenuTree( $menu, $indent = 0, $sub = false ) {
  $output = '';
  if ( is_array($menu) && count($menu) ) {
    foreach ($menu as $key => $value) {
      if ( is_array($value) ) {
        $output .= str_repeat("\t",$indent).'<li id="'.$key.
                   '" class="file ext_txt"><a href="'.
                   $value['page'].'">'.$value['alt']."</a>";
        $output .= multiArray2MenuTree($value, $indent+1, true);
        $output .= "</li>\n";
      } elseif ($key == 'alt' && !$sub) {
        $output .= str_repeat("\t",$indent).'<li id="'.$key.
                   '" class="directory collapsed"><a href="#"
                   class="toggle">'.$value['alt']."</a></li>\n";
      }
    }
    if ( strlen($output) > 0 ) {
      $output = PHP_EOL.str_repeat("\t",$indent).
		'<ul class="navTree toggleClosed">'.
      		PHP_EOL.$output.PHP_EOL.
		str_repeat("\t",$indent).'</ul>';
    }
  }
  return $output;
}

echo multiArray2MenuTree($navArray);

Try the above and let us know how it worked.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux