递归下拉菜单在Bootstrap Codeigniter上无法正确显示

时间:2015-07-29 12:03:24

标签: php css twitter-bootstrap codeigniter

我在使用数据库显示由递归函数生成的下拉菜单时遇到问题。父项在项目下方正确显示,但不是导航项目,而是作为基本列表。

以下是图片的实际外观:enter image description here

以下是它应该如何看待:

enter image description here

一些相关代码:

型号:

<?php
class New_menu_model extends CI_Model {
    function get_domains() {
        $result = $this->db->get ( 'domenii' );
        return $result->result_array();
    }
}
    function recursive($parent, $result) {

        $has_children = false;
        foreach ( $result as $key => $value ) {
            if ($value ['parent'] == $parent) {
                if ($has_children === false && $parent) {
                    $has_children = true;
                    echo '<ul>' . "\n" ;
                }
                echo '<li>' . "\n";
                echo '<a href="/menu/domenii/' . $value ['id_domeniu'] . '">' . $value ['nume_domeniu'] . '</a>' . " \n";
                echo "\n";
                recursive ( $key + 1, $result );
                echo "</li>\n";
            }
        }
        if ($has_children === true && $parent)
            echo "</ul>\n";
    }

?>

查看:

 <li class="dropdown"><?php echo recursive(0, $menu); ?></li>

谢谢!

1 个答案:

答案 0 :(得分:0)

应该是这样的:

<li class="dropdown">

     <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
          Dropdown <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
         <?php echo recursive(0, $menu); ?>
       </ul>
</li>

...并检查echo '<ul>' . "\n" ;,它可能是echo '<ul class="dropdown-menu">' . "\n" ;所以代码应为:

 <li class="dropdown">

         <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
              Dropdown <span class="caret"></span>
            </a>

             <?php echo recursive(0, $menu); ?>

    </li>