sql – Dynamic Menu In PHP & MySQL, Grouping Menu & Submenu-ThrowExceptions

Exception or error:

Hello there i was trying to create dynamic navigation menu in php..

it was like

1.Home
2.About us
3.Services
  3a. Service One
  3b. Service Two
  3c. Service Three
  3d. Service Four
4. Contact us

in above example type i want to generate dropdown.

i know how to generate without dropdown it will be …

$query_fetch_all_cat = "SELECT * FROM categories";
$result_fetch_all_cat = mysqli_query($connection, $query_fetch_all_cat);
if(!$result_fetch_all_cat){
    die("error while creating categories" . mysqli_error($connection));
} else {
    while ($row = mysqli_fetch_assoc($result_fetch_all_cat)) {
        $li_id = $row['cat_id'];
        $li = $row['cat_title'];
        echo "<li><a href='categories.php?view_cat=$li_id'>{$li}</a></li>";
    }
}

In process of i am trying to create multiple dropdown i am failed Here the code that was i try to create multiple dropdown.

function get_menus() {
        $connection = connect();
        $fetch_dropdown_menus = "SELECT * FROM dropdown_nav";
        $result_dropdown_menus = mysqli_query($connection, $fetch_dropdown_menus);

        if(mysqli_num_rows($result_dropdown_menus)) {

                $rows = mysqli_fetch_all($result_dropdown_menus, MYSQLI_ASSOC);
                $menu = [];

                foreach ($rows AS $index => $row) {

                        if($row['menu_id']){
                            $id = $row['menu_id'];

                            $menus['menu_'.$id]['submenu'][] = [
                                'id' => $row['nav_id'],
                                'title' => $row['nav_title'],
                                'page' => $row['page'],
                            ];
                        }
                         else {
                            $id = $row['nav_id'];

                            $menus['menu_'.$id] = [
                                'id' => $row['nav_id'],
                                'title' => $row['nav_title'],
                                'page' => $row['page'],
                            ];
                        }
                }

                return $menus;

        } else {
            echo "Data not there";
        }

    }
            function display_menu(){
            $menus = get_menus();

            if(!$menus){
                return 'No Menu exists in databse';
            }

            $html = '';
            $html.= '<ul>';

            foreach ( $menus AS $menu ) {
                if(isset($menu['submenu'])){

                    if($menu['page']){
                        $html.= '<li><a href="'.$menu['page'].'">'.$menu['title'].'</a>
                        ';
                    } else {
                        $html.= '<li class="drop-down"><a href="#">'.$menu['title'].'</a>';
                    }

                    if(is_array($menu['submenu'])){

                        $html.= '<ul>';
                        foreach ($menu['submenu'] AS $submenu) {

                            if($submenu['page']){
                                $html.= '<li><a href="'.$submenu['page'].'">'.$submenu['title'].'</a></li>';
                            } else {
                                $html.= '<li><a href="#">'.$submenu['title'].'</a></li>';
                            }
                        }

                        $html.='</ul>';
                    }
                        $html.='</li>';



                } else {

                if($menu['page']){
                    $html.= '<li><a href="'.$menu['page'].'">'.$menu['title'].'</a></li>';
                } else {
                    $html.= '<li><a href="#">'.$menu['title'].'</a></li>';
                }
            }
        };

so how i can generate multiple dropdowns am i missing something… Please let me know

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *