Ja sam to rjesio tako sto sam napunio niz kategorija i dodao ovu funkciju:
Code:
private function build_tree()
{
$refs = array();
$list = array();
$arr = $this->categories;
foreach($arr as $data)
{
$thisref = & $refs[ $data['id'] ];
$thisref['parent_id'] = $data['parent_id'];
$thisref['name'] = $data['name'];
if ($data['parent_id'] == 0) {
$list[ $data['id'] ] = &$thisref;
} else {
$refs[ $data['parent_id'] ]['children'][ $data['id'] ] = &$thisref;
}
}
return $list;
}
Zatim primjer upotrebe ovoga:
Code:
public function get_cat_options($selected_id = null)
{
$arrTree = $this->build_tree();
$html = '<option value="">-----</option>';
foreach($arrTree as $id => $data)
{
$html .= $this->print_option($id, $data, 0 , $selected_id );
}
return $html;
}
private function print_option($id , $data, $lvl , $selected_id = null )
{
$html = '<option value="' . $id . '" ' . ($selected_id==$id ? 'selected' : '') . '>' . str_repeat(' ',$lvl ) . $data['name'] . '</option>';
if( @count($data['children']))
{
foreach($data['children'] as $k => $v)
{
$html .= $this->print_option($k,$v,$lvl+1 , $selected_id);
}
}
return $html;
}