简体   繁体   English

具有Bootstrap导航的Codeigniter递归类别菜单

[英]Codeigniter Recursive Category Menu with Bootstrap Navigation

Database: 数据库:

CREATE TABLE IF NOT EXISTS `category` (
  `cat_id` int(11) NOT NULL AUTO_INCREMENT,
  `cat_name` varchar(32) DEFAULT NULL,
  `cat_name_sef` varchar(64) DEFAULT NULL,
  `parent_id` int(11) DEFAULT '0',
  PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

I need help for my recursive codeigniter function. 我的递归codeigniter函数需要帮助。

Below is my function code: 下面是我的功能代码:

function categories($parent_id = 0)
    {
        $sql = $this->db->where('parent_id', $parent_id)->order_by('cat_name', 'ASC')->get('category');
        $html = '';
        foreach ($sql->result() as $row)
        {
            $this->db->where('parent_id', $row->cat_id);
            if ($this->db->count_all_results('category') != 0)
            {
                $html .= '<li><a href="#">' . $row->cat_name . '</a>';
                $html .= $this->categories($row->cat_id);
            }
            else $html .= '<ul><li>' . anchor(array('category', $row->cat_id, $row->cat_name_sef), $row->cat_name) . '</li></ul>';
            $html .= '</li>';
        }
        return $html;
    }

This function simple output is like below: 该函数的简单输出如下所示:

<li><a href="#">Smartphones</a>
    <ul><li><a href="http://localhost/project/index.php/category/1/apple">Apple</a></li></ul></li>
    <ul><li><a href="http://localhost/project/index.php/category/2/samsung">Samsung</a></li></ul></li>
    <ul><li><a href="http://localhost/project/index.php/category/3/motorola">Motorola</a></li></ul></li>
    <ul><li><a href="http://localhost/project/index.php/category/4/nokia">Nokia</a></li></ul></li>

I want to do this output as like below: 我想这样做,如下所示:

                <li><a href="#">Smartphones</a>
                    <ul>
                        <li><a href="http://localhost/project/index.php/category/1/apple">Apple</a></li>
                        <li><a href="http://localhost/project/index.php/category/2/samsung">Samsung</a></li>
                        <li><a href="http://localhost/project/index.php/category/3/motorola">Motorola</a></li>
                        <li><a href="http://localhost/project/index.php/category/4/nokia">Nokia</a></li>
                    </ul>
                </li>

Hope that I explain my issue. 希望我能解释我的问题。 Thank you so much for advice. 非常感谢您的建议。

there are many ways to do this. 有很多方法可以做到这一点。 lets start (add your html yourself) 让我们开始(自己添加html)

this should work. 这应该工作。 It is not recusrive because there is no point! 这是没有意义的,因为没有意义!

$sql = $this->db->order_by('cat_name', 'ASC')->get('category'); //grab all categories and subcategories together!

foreach ($sql->result() as $row) {

    if ($row->parent_id == '0') {
        //we have a category
        echo $row->cat_name;

        foreach ($sql->result() as $subcat) {

            if ($subcat->parent_id == $row->cat_id) {

                //we have a subcategory
                echo $subcat->cat_name;

            }

        }

    }

}

EDIT 编辑

$html = ""; //init

foreach ($sql->result() as $row) {
    $html .= "<li>";

    if ($row->parent_id == '0') {
        //we have a category
        $html .= "<a>" . $row->cat_name . "</a>";
        $html .= "<ul>";
        foreach ($sql->result() as $subcat) {

            if ($subcat->parent_id == $row->cat_id) {
                //we have a subcategory
                $html .= "<li><a>".$subcat->cat_name."</a></li>";
            }

        }
        $html .= "</ul>";

    }

    $html .= "</li>";

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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