dongwuli5105 2016-02-01 11:05
浏览 121
已采纳

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... :)

  • 写回答

1条回答 默认 最新

  • doukun1450 2016-02-01 11:12
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了