简体   繁体   English

如何使用CSS,MySQL和Codeigniter制作树菜单

[英]How to make tree menu with css, mysql and codeigniter

i wanna make dynamic tree menu using css and codeigniter. 我想使用CSS和Codeigniter制作动态树菜单。 here is table of cmb_menu: 这是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')

and i use this function to get normal result. 我用这个功能来得到正常的结果。 and it works. 而且有效。

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

but i get a problem when using it and i have no idea how to customize that function so i can get the result. 但是我在使用它时遇到了一个问题,我也不知道如何自定义该功能,以便获得结果。 like this: 像这样:

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

MySQL isn't able to execute recursive queries. MySQL无法执行递归查询。 You have two choices : 您有两种选择:

  • Change your model to The Nested Set Model as explained into this excellent article . 这篇出色的文章中所述,将您的模型更改为“嵌套集模型
  • Create a procedure that will allow you to explore your tree (see code below). 创建一个过程,使您可以探索树(请参见下面的代码)。

Then you just have to call it by passing to it the parent id and iterate on the resulting rows (each row is a node and the second column is the list of ids of the children nodes). 然后,您只需通过将其传递给父ID并对其进行迭代就可以对其进行调用(每行是一个节点,第二列是子节点的ID列表)。 You can change the code of the function so as to add more information. 您可以更改功能代码,以添加更多信息。

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

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

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