I was looking at implementing the recursive function from this SO item: PHP: nested menu with a recursive function, expand only some nodes (not all the tree) to build a right click menu on my application and need to get the nested level. I am trying to think through the workflow here but can't think of where to put my increment by one and reset it to end up with variables such as:
$nestedLevel = 1; // main item
$nestedLevel = 2; // sub menu
$nestedLevel = 2; // sub-sub menu
I would like to use that to setup some if statements to I can handle each level differently. the Main level will be a simple list, Level 2 would be a horizontal button group, and the third will be simple lists assigned to tabs from the button group. Here is what I have:
function recursive($parent, $array) {
$has_children = false;
foreach($array as $key => $value) {
if ($value['menu_parent_id'] == $parent) {
if ($has_children === false && $parent) {
$has_children = true;
echo '<ul>' ."\n";
}
echo '<li>' . "\n";
echo '<a href="/page.php?id=' . $value['menu_item_id'] . '">' . $value['name'] . '</a>' . " \n";
echo "\n";
recursive($key, $array);
echo "</li>\n";
}
}
if ($has_children === true && $parent) echo "</ul>\n";
}
In the end it will look something like this (with some more styling of course):