2014-05-25 43 views
0

路徑我有一個分類模型採用這種結構:如何創建嵌套的父子結構

ID,姓名,PARENT_ID

我想創建一個breacrumb風格的路徑,但我不知道怎麼辦。 請幫助我。

謝謝。

+0

目前爲止的相關代碼? – kaiyaq

回答

1

你給我們的工作很少。

以下代碼可用於獲取下拉樹。你可以調整它來適應你的麪包屑的例子。

/** 
* Create a tree dropdown based on the parent child relationships 
* 
* @param $parents Array of Category models to draw list for 
* @return array listitem with populated tree. 
* 
* @access public 
*/ 
public function makeDropDown($parents) 
{ 
    global $listItems; 
    $listItems = array(); 
    $listItems['0'] = '== Choose a Category =='; 
    foreach ($parents as $parent) { 
     $listItems[$parent->category_id] = $parent->category_name; 
     $this->subDropDown($parent->categories); 
    } 
    return $listItems; 
} 

/** 
* Create a tree dropdown based of a child 
* 
* @param $children Array of children models to draw list for 
* @param $space String identation string 
* @return array listitem with populated tree. 
* 
* @access private 
*/ 
private function subDropDown($children, $space = '---') 
{ 
    global $listItems; 

    foreach ($children as $child) { 

     $listItems[$child->category_id] = $space . $child->category_name; 
     $this->subDropDown($child->categories, $space . '---'); 
    } 
}