2014-09-26 47 views
0

我想使用CSS和笨進行動態樹形菜單樹菜單。 這裏是cmb_menu表:
如何使CSS,MySQL和笨

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

回答

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

我要去試試這個 – Armen 2014-09-26 16:55:24