Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//----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

This comment has been minimized.

Copy link

@avenirer avenirer commented Oct 20, 2015

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:

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 $this->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;
    }
@PetengDedet

This comment has been minimized.

Copy link
Owner Author

@PetengDedet PetengDedet commented Oct 20, 2015

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>";
@avenirer

This comment has been minimized.

Copy link

@avenirer avenirer commented Oct 20, 2015

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

This comment has been minimized.

Copy link
Owner Author

@PetengDedet PetengDedet commented Oct 20, 2015

yeah, but it give me nothing

@avenirer

This comment has been minimized.

Copy link

@avenirer avenirer commented Oct 20, 2015

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

@avenirer

This comment has been minimized.

Copy link

@avenirer avenirer commented Oct 20, 2015

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