如何使用css,mysql和codeigniter制作树状菜单

时间:2014-09-26 11:52:39

标签: php html mysql css codeigniter

我想使用css和codeigniter制作动态树菜单。 这是cmb_menu表:

(`id_menu`, `id_parent`, `menu`)
(1, '0', 'see and do')
(2, '0', 'travel info')
(3, '0', 'inside dublin')
(4, '1', 'event')
(5, '1', 'tour')
(6, '2', 'tips')

我使用此功能获得正常结果。它有效。

function menu($parent=0,$hasil){

        $w = $this->db->query("SELECT * from cmb_menu where id_parent='".$parent."'");

        if(($w->num_rows())>0)

        {
            $hasil .= "<ul>";
        }
        foreach($w->result() as $h)
        {
            $hasil .= "<li>".$h->menu;

            $hasil = $this->menu($h->id_menu,$hasil);

            $hasil .= "</li>";
        }
        if(($w->num_rows)>0)

       {
            $hasil .= "</ul>";
        }
        return $hasil;
    }

但是我在使用它时遇到问题,我不知道如何自定义该功能,所以我可以得到结果。像这样:

<ul class="menu span9 inline">
<li class="dropdown-submenu">
                <a href="travel.html">see and do</a>
                <ul class="dropdown-menu">
                    <li><a href="">Highlights</a></li>
                    <li class="dropdown-submenu">
                        <a href="">Activities</a>
                        <ul class="dropdown-menu">
                            <li><a href="">Traditional</a></li>
                            <li><a href="">Shopping</a></li>
                            <li><a href="">Cafes</a></li>
                            <li><a href="">Restaurants</a></li>
                        </ul>
                    </li>
                    <li><a href="">Events</a></li>
                    <li><a href="">Tour & Attractions</a></li>
                </ul>
            </li>
            <li class="dropdown-submenu">
                <a href="travel.html">where to stay</a>
                <ul class="dropdown-menu">
                    <li><a href="">Hotel</a></li>
                    <li><a href="">Homestay</a></li>
                    <li><a href="">Guesthouse</a></li>
                </ul>
            </li>
            <li><a href="fashion.html">inside minangkabau</a></li>
            <li><a href="travel.html">travel info</a></li>
            <li class="dropdown-submenu">
                <a href="fashion.html">articles</a>
                <ul class="dropdown-menu">
                    <li><a href="">Tips</a></li>
                    <li><a href="">Offers</a></li>
                    <li><a href="">Minangkabau</a></li>
                    <li><a href="">Culture</a></li>
                    <li><a href="">Food & Drink</a></li>
                </ul>
            </li>
            <li><a href="travel.html">map</a></li>
            </ul>

1 个答案:

答案 0 :(得分:0)

MySQL无法执行递归查询。你有两个选择:

  • 将模型更改为嵌套集模型,如this excellent article中所述。
  • 创建一个允许您浏览树的过程(参见下面的代码)。

然后你只需要调用它来传递父id并迭代生成的行(每行是一个节点,第二列是子节点的id列表)。您可以更改函数的代码以添加更多信息。

DELIMITER $$

DROP FUNCTION IF EXISTS `GetFamilyTree` $$
FUNCTION `GetFamilyTree`(`GivenID` INT) RETURNS varchar(1024) CHARSET latin1
DETERMINISTIC
BEGIN

DECLARE rv,q,queue,queue_children VARCHAR(1024);
DECLARE queue_length,front_id,pos INT;

SET rv = '';
SET queue = GivenID;
SET queue_length = 1;

WHILE queue_length > 0 DO
    SET front_id = FORMAT(queue,0);
    IF queue_length = 1 THEN
        SET queue = '';
    ELSE
        SET pos = LOCATE(',',queue) + 1;
        SET q = SUBSTR(queue,pos);
        SET queue = q;
    END IF;
    SET queue_length = queue_length - 1;

    SELECT IFNULL(qc,'') INTO queue_children
    FROM (SELECT GROUP_CONCAT(id) qc
    FROM cmb_menu WHERE id_parent = front_id) A;

    IF LENGTH(queue_children) = 0 THEN
        IF LENGTH(queue) = 0 THEN
            SET queue_length = 0;
        END IF;
    ELSE
        IF LENGTH(rv) = 0 THEN
            SET rv = queue_children;
        ELSE
            SET rv = CONCAT(rv,',',queue_children);
        END IF;
        IF LENGTH(queue) = 0 THEN
            SET queue = queue_children;
        ELSE
            SET queue = CONCAT(queue,',',queue_children);
        END IF;
        SET queue_length = LENGTH(queue) - LENGTH(REPLACE(queue,',','')) + 1;
    END IF;
END WHILE;

RETURN rv;

END$$