具有引导程序导航的Codeigniter递归类别菜单

时间:2014-02-10 19:14:53

标签: php codeigniter twitter-bootstrap recursion

数据库:

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;

我的递归codeigniter函数需要帮助。

以下是我的功能代码:

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;
    }

此功能简单输出如下:

<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>

我想按以下方式执行此输出:

                <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>

希望我解释一下我的问题。非常感谢您的建议。

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。让我们开始(自己添加你的HTML)

这应该有效。它没有重新开始,因为没有意义!

$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;

            }

        }

    }

}

编辑

$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>";

}