PHP,Bootstrap导航栏从数据库加载项目..如何添加类下拉列表?

I'm making a web site with php and bootstrap... I've loaded navitems from database and applied bootstrap style to it.. I'm having issues with the "dropdown" class because I honestly don't know how to write an if statement that would do something like "if $subject has $page, add class dropdown to $subject"..$subject are the main navitems and $page are the items that are visible when the dropdown is clicked... right now when the $subject without $page is clicked it shows an empty box a few px high and about 40px wide..

here's my code...

    <ul class="nav navbar-nav">

        <?php $subject_set = find_all_subjects(); ?>

        <?php

            while($subject = mysqli_fetch_assoc($subject_set)) {

        ?>
        <?php 
            echo "<li class=\"navitem dropdown" ;
            if($subject["id"] == $selected_subject_id) {
                echo " active\"" ;
            }else{
                echo "\"" ;
            }
            echo ">" ;
        ?>
        <a href="admin.php?subject=<?php echo urlencode($subject["id"]); ?>">
        <?php echo $subject["menu_name"]; ?>
        </a>

        <?php $page_set = find_pages_for_subject($subject["id"]) ; ?>

            <ul class="dropdown-menu" role="menu">
                <?php
                    while($page = mysqli_fetch_assoc($page_set)) {
                ?>
                    <li>
                    <a href="admin.php?page=<?php echo urlencode($page["id"]); ?>"><?php echo $page["menu_name"]; ?> </a>
                    </li>
                <?php
                    }
                ?>
                <?php
                    mysqli_free_result($page_set);
                ?>
            </ul>

        </li>
        <?php
            }
        ?>
    <?php
        mysqli_free_result($subject_set);
    ?>

    </ul>

I would be very grateful if someone would write that code for me... :)

Keep in mind that I'm a noob at this...be gentle... :)

First thing is that you could load children just after entering into your page loop :

while($subject = mysqli_fetch_assoc($subject_set)) {
    $page_set = find_pages_for_subject($subject["id"]);
    $nbPages = mysqli_num_rows($page_set);

That way you get the number of children with the $nbPages variable

Then, when you output the page list, you would be able to determine if a dropdown class is needed :

echo "<li class=\"navitem" . ($nbPages ? 'dropdown' : '') . "\"">;

Same thing for outputing the children list :

if ($nbPages) {
    ?>
    <ul class="dropdown-menu" role="menu">
    ....
    <?php
}