简体   繁体   中英

How to make tree menu with css, mysql and codeigniter

i wanna make dynamic tree menu using css and codeigniter. here is table of 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. 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). 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$$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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