Reputation: 911
I tried to create multilevel menu using codeigniter these are the two functions i used to echo the menu.
function loop_array($array = array(), $parent_id = 0) {
if (!empty($array[$parent_id])) {
// echo '<ul>';
foreach ($array[$parent_id] as $items) {
echo '<li><a class = "parent"><span>';
echo $items['label'];
echo '</span></a>';
loop_array($array, $items['id']);
echo '</li>';
}
echo '</ul>';
}
}
function display_menus($menu_items) {
// var_dump($menu_items);
$array = array();
foreach ($menu_items as $rows) {
$array[$rows['parent']][] = $rows;
}
loop_array($array);
}
then the out put html code is like
<div id='Mymenu' style=' color: #000; width: 960px; background-image:url(); clear: both; text-align: center; height: 46px;'>
<div id="menu">
<ul class="menu">
<li><a class = "parent"><span>Home</span></a></li><li><a class = "parent"><span>Code</span></a><li><a class = "parent"><span>PHP</span></a><li><a class = "parent"><span>Scripts</span></a><li><a class = "parent"><span>Archive</span></a><li><a class = "parent"><span>Snippet</span></a></li></ul></li></ul></li><li><a class = "parent"><span>Help</span></a></li></ul></li><li><a class = "parent"><span>CSS</span></a></li></ul></li><li><a class = "parent"><span>Contact</span></a></li></ul> </ul>
</div>
</div>
but i need to make this as
<div id='Mymenu' style=' color: #000; width: 960px; background-image:url(); clear: both; text-align: center; height: 46px;'>
<div id="menu">
<ul class="menu">
<li><a href="index" class="parent"><span>HOME</span></a>
<div><ul>
<li><a href=""><span>Vertical Menu</span></a>
<li><a href=""><span>Slide show</span></a>
</li>
<li><a href="#" class="parent"><span>ABOUT US</span></a></li>
<li><a href="news"><span>NEWS</span></a></li>
<li class="last"><a href="#"><span>ACADEMICS</span></a></li>
<li class="last"><a href="#"><span>PROGRAMME</span></a></li>
<li class="last"><a href="#"><span>ADMISSIONS</span></a></li>
<li class="last"><a href="courses"><span>COURSES</span></a></li>
<li class="last"><a href="contact_us"><span>CONTACT US</span></a></li>
<li class="last"><a href="feedback"><span>Feedback</span></a></li>
</ul>
</div>
can anyone give me a way to do this?
Upvotes: 0
Views: 2065
Reputation: 521
Please note that your target code is faulty html to begin with (not closed html tags) and I am not sure if it's really what you need or want, ie is it really the ultimate correct form?
Further if you formulate your problem clearly and have the code in a readable format (instead of on one line) others don't have waste time to understand what you want in the first place.
I assume you have a tree-like / multilevel menu which you want to print out differentiating between internal nodes (ie nodes with children, or "parents") and terminal nodes (ie your "last" elements).
Your *display_menus* function groups the menu items by its parent menu id, and seems correct.
Your *loop_array* function seems to be almost there:
function loop_array($array = array(), $parent_id = 0)
{
if (!empty($array[$parent_id]))
{
echo "\n<ul>";
foreach ($array[$parent_id] as $items)
{
// is it a terminal node?
if (empty($array[$items['id']]))
{
$class = 'last';
}
else
{
$class = 'parent';
}
// is there a link?
if (empty($items['my-link-field']))
{
$link = '#';
}
else
{
$link = $items['my-link-field'];
}
// if you do not divide your output string into several statements,
// the code becomes more readable
// I mean
// it
// feels
// like
// several
// fragments
// that
// must
// be seen, understood and put into context. Which -in any language- is just easier with a nice flow.
echo "<li><a class='{$class}' href='{$link}'><span>{$items['label']}</span></a>";
loop_array($array, $items['id']);
echo "</li>\n";
}
echo "</ul>\n";
}
}
If you still do not get the desired result, I would assume your data ($menu_items) to be incorrectly set up. As you seem to have checked that already (commented var_dump), you might want to check the data passed to loop_array, ie $array.
Upvotes: 1