简体   繁体   中英

Bootstrap dropdown nav only working with current page in dynamic navigation

I build a website while learning PHP, SQL etc. I am now trying to build on the original page I have made every works fine with the standard navigation, when I try to add a bootstrap navbar the dropdown box only appears for pages I am currently on. On index.php none of the dropdowns work. The page has a CMS in the back end and I am trying to have the public side of the side just look nicer, and ensure that no matter how many new pages or subjects are in the database the css is applied to everything.

any assistance will be greatly appreciated!

bonus question: I would also like the carats to only appear when the "subject" has more than one page associated with it if that's possible, i cant figure out how to make that work either. eg: so when clicking on the subject you goto its only page on click, however if there is more than 1 page, a carrat appears and the dropdown menu is where you select your desired page from

My navigation is a function and works fine its only the css causing my initial issue.

function public_navigation($subject_array, $page_array) {
        $output = "<nav class=\"navbar navbar-default\">";
        $output .= "<div class=\"container-fluid\">";
        $output .= "<div class=\"navbar-header\">";
        $output .= "<button type=\"button\" class=\"navbar-toggle\" data-toggle=\"collapse\" data-target=\"#myNavbar\">";
        $output .= "<span class=\"icon-bar\"></span>";
        $output .= "<span class=\"icon-bar\"></span>";
        $output .= "<span class=\"icon-bar\"></span>";
        $output .= "</button>";
        $output .= "<a class=\"navbar-brand\" href=\"index.php\">Company</a>";
        $output .= "</div>";
        $output .= "<div class=\"collapse navbar-collapse\" id=\"myNavbar\">";
        $output .= "<ul class=\"nav navbar-nav\">";
        $subject_set = find_all_subjects();
        while($subject = mysqli_fetch_assoc($subject_set)) {
            $output .= "<li class=\"dropdown\">";
            $output .= "<a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"index.php?subject=";
            $output .= urlencode($subject["id"]);
            $output .= "\">";
            $output .= htmlentities($subject["menu_name"]);
            $output .= "<span class=\"caret\"></span></a>";

            if ($subject_array["id"] == $subject["id"] || 
                $page_array["subject_id"] == $subject["id"]) {
                $page_set = find_pages_for_subject($subject["id"]);
                $output .= "<ul class=\"dropdown-menu\">";
                while($page = mysqli_fetch_assoc($page_set)) {
                    $output .= "<li>";
                    $output .= "<a href=\"index.php?page=";
                    $output .= urlencode($page["id"]);
                    $output .= "\">";
                    $output .= htmlentities($page["menu_name"]);
                    $output .= "</a></li>";
                }
                $output .= "</ul>";
                mysqli_free_result($page_set);
            }

            $output .= "</li>"; // end of the subject li
        }
        mysqli_free_result($subject_set);
        $output .= "</ul></div></div></nav>";
        return $output;
    }

EDIT: This is what the menu looks like

but if you click on anyof the other headings no dropdown box appears, and if on index.php with the generic welcome page none of the menus have dropdown boxes or let you navigate to them

Without knowing what is being passed in your $subject_array and $page_array it will be hard to say why this is failing, but almost certainly the problem is showing up in your if statement:

if ($subject_array["id"] == $subject["id"] || $page_array["subject_id"] == $subject["id"]) {

It looks like you don't need the if statement at all since you want to get the pages for the given subject every time. So I suggest you try removing the if and see what you get:

        $output .= "<span class=\"caret\"></span></a>";

        $page_set = find_pages_for_subject($subject["id"]);
        $output .= "<ul class=\"dropdown-menu\">";
        while($page = mysqli_fetch_assoc($page_set)) {
            $output .= "<li>";
            $output .= "<a href=\"index.php?page=";
            $output .= urlencode($page["id"]);
            $output .= "\">";
            $output .= htmlentities($page["menu_name"]);
            $output .= "</a></li>";
        }
        $output .= "</ul>";
        mysqli_free_result($page_set);

        $output .= "</li>"; // end of the subject li

now you'll have your dropdown menus for each subject and you only need to have a small if statement to add some extra CSS class or something to show which subject is the currently selected subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM