First is this Array code valid, I think it's probably not. What I am trying to do is loop through the array to build a Navigation Menu. The buildMenu function will loop the main_menu array but not the sub_menu array, I'm just a PHP Learner and am lost with this any help would be great. Thanks
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
}
{ echo "</ul><!-- Sub Menu Unordered List Item Closing Tag -->";
}
} { echo"\n</li><!-- Sub Menu List Item Closing Tag -->\n";
//echo "</li>";
}
}
echo "\n</ul><!-- Unordered List End Tag -->\n";
Code:
<ul id="nav_menu"><!-- Unordered List Start Tag -->
<li class="main_menu"><a href='index.php'>HOME PAGE</a>
<ul class="sub_menu">
<li><a href='H'>H</a></li>
<li><a href='i'>i</a></li>
</ul><!-- Sub Menu Unordered List Item Closing Tag -->
</li><!-- Sub Menu List Item Closing Tag -->
<li class="main_menu"><a href='index.php?id=2'>MENU 2</a>
<ul class="sub_menu">
<li><a href='M'>M</a></li>
<li><a href='i'>i</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 1</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 2</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 3</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 4</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 5</a></li>
</ul><!-- Sub Menu Unordered List Item Closing Tag -->
</li><!-- Sub Menu List Item Closing Tag -->
<li class="main_menu"><a href='index.php?id=3'>MENU 3</a>
<ul class="sub_menu">
<li><a href='M'>M</a></li>
<li><a href='i'>i</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 1</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 2</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 3</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 4</a></li>
<li><a href='index.php?id=xxxxxxxxxxx'>Sub Menu 5</a></li>
</ul><!-- Sub Menu Unordered List Item Closing Tag -->
</li><!-- Sub Menu List Item Closing Tag -->
<li class="main_menu"><a href='index.php?id=4'>MENU 4</a>
<ul class="sub_menu">
<li><a href='M'>M</a></li>
<li><a href='i'>i</a></li>
</ul><!-- Sub Menu Unordered List Item Closing Tag -->
</li><!-- Sub Menu List Item Closing Tag -->
<li class="main_menu"><a href='index.php?id=5'>MENU 5</a>
<ul class="sub_menu">
<li><a href='M'>M</a></li>
<li><a href='i'>i</a></li>
</ul><!-- Sub Menu Unordered List Item Closing Tag -->
</li><!-- Sub Menu List Item Closing Tag -->
</ul><!-- Unordered List End Tag -->
I have 2 issues, first should the Menu's that dont have a Sub menu array not have closing </li> tag's ,for example the first Home link <li class="main_menu"><a href='index.php'>HOME PAGE</a>
should it be <li class="main_menu"><a href='index.php'>HOME PAGE</a></li>
The second issue is for some reason the Loop is creating 2 unwanted List items at the start of each UL , example
I have 2 issues, first should the Menu's that dont have a Sub menu array not have closing </li> tag's ,for example the first Home link <li class="main_menu"><a href='index.php'>HOME PAGE</a>
should it be <li class="main_menu"><a href='index.php'>HOME PAGE</a></li>
Yes, it should has closing tag
Quote:
Originally Posted by trazix
The second issue is for some reason the Loop is creating 2 unwanted List items at the start of each UL , example
echo "\n\n\n<ul id=\"nav_menu\"><!-- Unordered List Start Tag -->\n";
foreach ($main_menu as $key => $keyValue) { echo "\n<li class=\"main_menu\"><a href='". $keyValue['link'] ."'>". $keyValue['name'] ."</a>"; if(is_array($keyValue)) { echo "\n\n<ul class=\"sub_menu\">\n"; foreach ($keyValue as $keyValuex) { echo "\n<li><a href='". $keyValuex['link'] ."'>". $keyValuex['name'] ."</a></li>\n"; } echo "</ul><!-- Sub Menu Unordered List Item Closing Tag -->"; } echo"\n</li><!-- Sub Menu List Item Closing Tag -->\n"; } echo "\n</ul><!-- Unordered List End Tag -->\n";
$main_menu[1][0] = array('name' => 'Sub Menu 1', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[1][1] = array('name' => 'Sub Menu 2', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[1][2] = array('name' => 'Sub Menu 3', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[1][3] = array('name' => 'Sub Menu 4', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[1][4] = array('name' => 'Sub Menu 5', 'link' => 'index.php?id=xxxxxxxxxxx');
$main_menu[2][0] = array('name' => 'Sub Menu 1', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[2][1] = array('name' => 'Sub Menu 2', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[2][2] = array('name' => 'Sub Menu 3', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[2][3] = array('name' => 'Sub Menu 4', 'link' => 'index.php?id=xxxxxxxxxxx'); $main_menu[2][4] = array('name' => 'Sub Menu 5', 'link' => 'index.php?id=xxxxxxxxxxx');
The H is from the first letter in first array e.g Home Page and the i is from index.php
This is the list of links as seen on webpage.
For some reason the loop is creating two Dud links in each menu list It's using the first letter of each array item.
Code:
HOME PAGE
H
i
MENU 2
M
i
Sub Menu 1
Sub Menu 2
Sub Menu 3
Sub Menu 4
Sub Menu 5
MENU 3
M
i
Sub Menu 1
Sub Menu 2
Sub Menu 3
Sub Menu 4
Sub Menu 5
MENU 4
M
i
MENU 5
M
i
function mm($main_menu,&$rets){ $rets.= '<ul>'; foreach($main_menu as $m){ $rets.= '<li>'.$m['name'].'</li>'; if(is_array($m['sub'])){ mm($m['sub'],$rets); } } $rets.= '</ul>'; }
mm($main_menu,$rets); echo $rets;
Edit:
hmmmm, works (e.g. displays OK in firefox) but I think my HTML is awry , think should be <li>ul>nested list NOT <li>blah></li><ul>nested list .. etc , will have a play and report back.
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
Last edited by firepages; 11-19-2012 at 05:34 AM..