Skip to content

Instantly share code, notes, and snippets.

@PetengDedet
Last active October 20, 2015 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PetengDedet/912f09e5faa14cc97cdc to your computer and use it in GitHub Desktop.
Save PetengDedet/912f09e5faa14cc97cdc to your computer and use it in GitHub Desktop.
//----MY Table--
MariaDB [sik]> select * from sik_institution;
+----+-------------------------------+-----------+--------+
| id | name | alias | parent |
+----+-------------------------------+-----------+--------+
| 1 | Kantor Bendahara Pesantren | bendahara | NULL |
| 2 | Bidang Pendidikan Tinggi | dikti | 1 |
| 3 | Institut Agama Islam Ibrahimy | iaii | 2 |
| 4 | Fakultas Syari'ah | fs | 3 |
+----+-------------------------------+-----------+--------+
4 rows in set (0.04 sec)
//-----MY_MODEL-------------------------
public function get_menu($id='0')
{
$this -> db -> where('id',$id);
$this -> db -> or_where('parent',$id);
$res_array = $this -> db -> get('institution') -> result_array();
return self::html_ordered_menu($res_array,NULL);
}
function html_ordered_menu($array,$parent_id = 0)
{
$menu_html = '<ul>';
foreach($array as $element)
{
if($element['parent']==$parent_id)
{
$menu_html .= '<li><a href="'.$element['id'].'">'.$element['name'].'</a>';
$menu_html .= self::html_ordered_menu($array,$element['id']);
$menu_html .= '</li>';
}
}
$menu_html .= '</ul>';
return $menu_html;
}
//---MY_CONTROLLER---------------------------
echo "<pre>";
//works well when i left the parameter blank
print_r($this -> Institution_model -> get_menu());
//give me nothing
print_r($this -> Institution_model -> get_menu(2));
echo "</pre>";
@avenirer
Copy link

Well... if you set get_menu(2) it's normal to receive only those institutions because you simply told the function that you want the elements that start from id 2, namely the elements that either have parent id 2 or id 2.

@PetengDedet
Copy link
Author

yeah, but it give me nothing

@avenirer
Copy link

return self::html_ordered_menu($res_array,$id);

@avenirer
Copy link

why are you using "self::" and not "$this->"? are you working with static methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment