0

Trying to construct a navigation using multi-dimensional arrays and recursion. I have the following code:

First I run <?php $title = 'pagename'; ?> on each individual page beneath doctype (for active class detection)

ARRAY:

<?php

$nav_array = array ('Home' => 'index.php',
                   'About' => array ( 'about.php', array (
                        'Michael' => array( 'michael.php', array (
                            'Blog' => 'blog.php',
                            'Portfolio' => 'portfolio.php')), 
                        'Aaron' => 'aaron.php' , 
                        'Kenny' => 'kenny.php', 
                        'David'=> 'david.php')),

                   'Services' => array ( 'services.php', array (
                        'Get Noticed' => 'getnoticed.php', 
                        'Hosting' => 'hosting.php')),

                   'Clients' => 'clients.php',
                   'Contact Us' => 'contact.php'
    );

    $base = basename($_SERVER['PHP_SELF']);
?>

FOREACH: (generates nav)

<ul>
<?php

foreach ($nav_array as $k => $v) {
    echo buildLinks ($k, $v, $base);    
}
?>
</ul>

buildLinks:

<?php // Building the links

function buildLinks ($label_name, $file_name, $active_class) {
    if ($label_name == $title) {
        $theLink = "<li><a class=\"selected\" href=\"$file_name\">$label_name</a></li>\n";
    } else {
        $theLink = "<li><a href=\"$file_name\">$label_name</a></li>\n";
    }

    return $theLink;
}


?>

Result: http://khill.mhostiuckproductions.com/siteLSSBoilerPlate/arraytest.php

The sub menu's will appear on the hover of parent element using CSS. I need to be able to fall through multiple sub-levels without modifying anything but the array.

How do I make my foreach fall through the rest of the array recursively?

(Note: that I have to have the ability to apply a class of active to current pages, and a class of arrow to parent elements that have a sub-menu present.)

2 Answers 2

1

No matter what data structure you use to build your navigation, you'll need to make your function recursive, here's a quick and dirty way:

echo "<ul>";
foreach ($nav_array as $nav_title => $nav_data) {
  echo buildLinks($nav_title, $nav_data, $base, $title);
}
echo "</ul>";

/* NOTE that we pass $title to the function */
function buildLinks ($label_name, $file_name, $active_class, $title) {

  $theLink = '';
  /* this is dirty code, you should reconsider your data structure */
  $navigation_list = false;
  if (is_array($file_name)) {
    $navigation_list = $file_name[1];
    $file_name = $file_name[0];
  }

  if ($active_class == $title) {
    $theLink = "<li><a class=\"selected\" href=\"$file_name\">$label_name</a></li>\n";
  } else {
    $theLink = "<li><a href=\"$file_name\">$label_name</a></li>\n";
  }

  if ($navigation_list) {
    $theLink .= "<ul>";
    foreach ($navigation_list as $nav_title => $nav_data) {
      $theLink .= buildLinks($nav_title, $nav_data, $active_class, $title);
    }
    $theLink .= "</ul>";
  }

  return $theLink;
}

Not a clean solution in anyway, if I were you I'd change the data structure to be an easier one to handle.

Sign up to request clarification or add additional context in comments.

2 Comments

The data structure meaning the array? Do you have any suggestions as to what path I should take on that? Something I can search for?
0

I think this is a very bad way. I recommend to save your menu elements as XML or JSON and use parsers. It will facilitate your work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.