Hi,
I'm having difficulty with a function I am trying to write, basically, my function accepts a pre-defined (hard-coded) array and generates a menu from it. The menu can have as many levels as possible which is the reason behind my thinking of using a recursive function.
An example of the array is as follows:
PHP Code:
$aMenu['aOptions']['activeClass'] = 'activeClass1';
$aMenu['aOptions']['inactiveClass'] = 'inactiveClass1';
$aMenu[1]['title'] = 'Home';
$aMenu[1]['url'] = 'link';
$aMenu[2]['title'] = 'MyPerformance';
$aMenu[2]['url'] = 'link';
$aMenu[2]['aOptions']['activeClass'] ='activeClass2';
$aMenu[2]['aOptions']['inactiveClass'] ='inactiveClass2';
$aMenu[2][0]['title'] = 'Child 1';
$aMenu[2][0]['url'] = 'link';
$aMenu[2][1]['title'] = 'Child 2';
$aMenu[2][1]['url'] = 'link';
$aMenu[2][2]['title'] = 'Child 3';
$aMenu[2][2]['url'] = 'link';
$aMenu[2][2]['aOptions']['activeClass'] ='activeClass3';
$aMenu[2][2]['aOptions']['inactiveClass'] ='inactiveClass3';
$aMenu[2][2][0]['title'] = 'Child 1';
$aMenu[2][2][0]['url'] = 'link';
$aMenu[2][2][1]['title'] = 'Child 2';
$aMenu[2][2][1]['url'] = 'link';
$aMenu[2][2][2]['title'] = 'Child 3';
$aMenu[2][2][2]['url'] = 'link';
$aMenu[3]['title'] = 'MyNotepad';
$aMenu[3]['url'] = 'link';
$aMenu[4]['title'] = 'OurBusiness';
$aMenu[4]['url'] = 'link';
$aMenu[5]['title'] = 'OurLibraries';
$aMenu[5]['url'] = 'link';
$aMenu[5]['aOptions']['activeClass'] = 'activeClass';
$aMenu[5]['aOptions']['inactiveClass'] = 'inactiveClass';
$aMenu[5][0]['title'] = 'Child 1';
$aMenu[5][0]['url'] = '';
$aMenu[5][1]['title'] = 'Child 2';
$aMenu[5][1]['url'] = '';
$aMenu[5][2]['title'] = 'Child 3';
$aMenu[5][2]['url'] = '';
$aMenu[6]['title'] = 'Reports';
$aMenu[6]['url'] = 'link';
$aMenu[7]['title'] = 'Dashboard';
$aMenu[7]['url'] = 'link';
As can be seen from the array each child menu needs to their own set of active and inactive styles. My intention was to detect the ['aOptions'] section to determine when a child menu starts.
Unfortunately the function I have been working on doesn't work or generate the output I require.
PHP Code:
foreach ($aMenu as $section=>$link) {
$ln.= '<ul>';
if (isset($link['aOptions'])) {
$ln.= $this->printMenu($link);
}
$ln.= '<li><a href='.$link['url'].'>'.$link['title'].'</li>';
$ln.= '</ul>';
} // end of foreach loop
I'm aware that this code isn't going to function correctly, but the question is how can I generate code which will correctly recurse and output the correct links with urls and titles.
Thanks
Bookmarks