Last active
October 20, 2015 08:20
-
-
Save PetengDedet/912f09e5faa14cc97cdc to your computer and use it in GitHub Desktop.
Here is my table structure
//------ sik_institution
MariaDB [sik]> select * from sik_institution;
MariaDB [sik]> select id,name,alias,parent 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 Syariah | fs | 3 |
| 5 | Bidang Pendidikan dan Pelajar | dikjar | 1 |
| 6 | SD Ibrahimy | sdi | 5 |
+----+-------------------------------+-----------+--------+
6 rows in set (0.00 sec)
and this is Institution_model
/* ----- application/models/Institution_models.php ----- */
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;
}
and this is the controller
i mean when i passed a parameter into get_menu($id)
function, it will give me institution where the id passed and it's childs .
So whenever i passed '2' into get_menu($id)
it give me institution 2 and 3.
/*----- application/controller/My_Controller.php --------- */
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>";
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.
yeah, but it give me nothing
return self::html_ordered_menu($res_array,$id);
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
Why did you put the function inside the MY_Model? You can change the value inside an extended class (in this case, a property) only from the class that extends it. The function, the way I wrote it, is not receiving a property, but a variable. ANYWAY... try these modified functions inside your institution_model.php and tell me if it works: