Welcome Guest, Not a member yet? Register   Sign In
Menu with subitems from DB - CI noob
#1

[eluser]Unknown[/eluser]
Hello,

being new to codeigniter (and not feeling comfortable around OOP & MVC just yet), I struggling with a particular page element - main menu driven from a db table.

The db table is in attachment.

Here is what I wish to achieve:

1. read and return all items from table that have parent_id = 0 (or just all items and filter the array later)
2. check if there are any records in table where parent_id=id of current record
3. if there are none render in view as <li> element, if there are some render in view as <li class="dropdown"> element with css class
4. if subitems found read all that records from db that has a parent_id=id and render

pseudocode would be something like that:

Code:
$q_parent=db->query(SELECT * FROM tbl_menus WHERE parent_id=0);
$r_parent=db->records_array($q_parent);
for each ($r_parent){
  $check = $r_parent['id'];
  $q_child=db->query(SELECT * FROM tbl_menus WHERE parent_id=$check);
  if ($q_child->numrows = 0){
         echo '<li>'.$r_parent['display_name'].'</li>'; //render list element
     } else {
    echo '<li class="dropdown">'.$r1['display_name'].'<ul>'; //reder list element & open internal <ul>
    $r_child=db->records_array($q_child);
    for each ($r_child){
       echo '<li>'.$r_child['display_name'].'</li>'; //render internal list element
    }
echo '</ul></li>'; // close internal <ul> and  dropown <li>
}

so far, using CI tutorials I am able to list the parent elements, bu nothing else
#2

[eluser]InsiteFX[/eluser]
Search the forums with my name I posted a lib to do just this a while back.
#3

[eluser]Unknown[/eluser]
Sad
I'm having some problems finding the right post. When you get few spare minutes on your watch could you possibly point me it the right direction. In the meantime I keep looking.


Thanks
#4

[eluser]InsiteFX[/eluser]
I cannot find it now either but this is the code that it was based on so should help you out.


Dynamic Dropdown Menu

#5

[eluser]noideawhattotypehere[/eluser]
Code:
private function _generate_menu($array, $arrayattrs = NULL, $parent = 0, $level = 0, $firstloop = TRUE) {
        if ($array == NULL) {
            return false;
        }
        global $html;
        $has_children = false;
        foreach ($array as $key => $value) {
            if ($value['parent_id'] == $parent) {
                if ($has_children === false) {
                    $has_children = true;
                    if ($firstloop && $arrayattrs != NULL) {
                        $attributes = '';
                        foreach ($arrayattrs as $attr) {
                            $attributes .= ' ' . $attr;
                        }
                        $html .= '<ul' . $attributes . '>';
                    } else {
                        $html .= '<ul>';
                    }
                    $level++;
                }
                $html .= '<li><a href="' . $value['address'] . '">' . $value['name'] . '</a>';
                $this->_generate_menu($array, NULL, $value['id'], $level, FALSE);
                $html .= '</li>';
            }
        }
        if ($has_children === true)
            $html .= '</ul>';
        return $html;
    }

call it like
Code:
$your_menu = $this->_generate_menu($array_with_pages);

you can also add array of lets say ids etc
Code:
$this->_generate_menu($array_with_pages, $array_with_attrs);
where $array_with_attrs is like array('id="my_menu_class"', 'class="my_menu_class"')

To add a class to items with submenus you could use jQuery or modify the function, heres jq example (with bonus 'active' class to link leading to current page, please note that .menu is a class for our menu, see above how to use it)
Code:
#script type="text/javascript"#
            jQuery(document).ready(function() {
                $("ul.menu li").children("ul").each(function() {
                    $(this).addClass("submenu");
                });
                $("ul.menu a").filter(function() {
                    return this.href == location.href.replace(/#.*/, "");
                }).parent().addClass("active");
            });
#/script#

PS: You could also change the function not to use global variables, i dont really like them but I'm too lazy to fix that for you.
PS2: You sure you want a class on li elements in submenu instead of nested ul's?




Theme © iAndrew 2016 - Forum software by © MyBB