使用php和mysql生成一个简单的第二级下拉菜单

时间:2017-11-13 12:41:23

标签: php mysql drop-down-menu menu dropdown

我有一个top_menu和一个名为table的almenu。我想通过菜单id-s获得一个简单的下拉菜单。这段代码的问题在于它回显了“Információk”菜单项3次,并从almenu表中输入了3个菜单项。

enter image description here

enter image description here

代码现在执行:(它从top_menu表中输出3次Információk菜单项作为导航栏中的新菜单项) Informáciok(Alapadatok),Informáciok(Kollégium),Informáciok(Osztályok)

而不是:Informáciok(Alapadatok,Kollégium,Osztályok)(这就是我想要的)

<?php
                $menu_sql = 
                "
                    SELECT 

                        top_menu.top_menu_nev,
                        top_menu.top_menu_seo,
                        top_menu.top_menu_dropdown,
                        almenu.almenu_nev,
                        almenu.almenu_seo

                    FROM top_menu LEFT JOIN almenu ON top_menu.top_menu_id = almenu.almenu_parent

                    WHERE top_menu.menu_status = 1 AND top_menu.menu_position = 1

                    ORDER BY top_menu.top_menu_sorrend ASC

                ";
                $get_menu = mysqli_query($kapcs, $menu_sql) or die(mysqli_error($kapcs));
                while($top_menu = mysqli_fetch_assoc($get_menu))
                {
                    if($top_menu['top_menu_dropdown'] == 0 )
                    {
                        echo '<li><a href="'.$host.'/'.$top_menu['top_menu_seo'].'" title="'.$top_menu['top_menu_nev'].'" class="top_menu_to_link">'.$top_menu['top_menu_nev'].'</a></li>';
                    }
                    else
                    {
                        echo '<li class="dropdown">';                       
                            echo '<a href="#" title="" class="top_menu_to_link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'.$top_menu['top_menu_nev'].'</a>'; 
                            echo '<ul class="dropdown-menu">';
                                echo '<li><a href="'.$host.'/'.$top_menu['almenu_seo'].'" title="'.$top_menu['almenu_nev'].'" class="top_menu_to_link">'.$top_menu['almenu_nev'].'</a></li>';
                            echo '</ul>';                   
                        echo '</li>';
                    }
                }
                ?>

1 个答案:

答案 0 :(得分:0)

问题是你一遍又一遍地循环整个导航。

以下代码可让您生成新的li项:

            echo '<li class="dropdown">';                       
            echo '<a href="#" title="" class="top_menu_to_link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'.$top_menu['top_menu_nev'].'</a>'; 
            echo '<ul class="dropdown-menu">';

            $get_menu = mysqli_query($kapcs, $menu_sql) or die(mysqli_error($kapcs));
            while($top_menu = mysqli_fetch_assoc($get_menu))
            {
                if($top_menu['top_menu_dropdown'] == 0 )
                {
                    echo '<li><a href="'.$host.'/'.$top_menu['top_menu_seo'].'" title="'.$top_menu['top_menu_nev'].'" class="top_menu_to_link">'.$top_menu['top_menu_nev'].'</a></li>';
                }
                else
                {
                            echo '<li><a href="'.$host.'/'.$top_menu['almenu_seo'].'" title="'.$top_menu['almenu_nev'].'" class="top_menu_to_link">'.$top_menu['almenu_nev'].'</a></li>';

                }
            }
            echo '</ul>';
            echo '</li>';