在php codeigniter中显示父级下的子记录

I have a Tbl containing below fields.I am able display only the first level and displaying fine.But I want to get the child details of each parent display under respective parent .please help me thanks in Advance.

id pid  name
1   0   proc
2   0   prod
3   0   dash
4   3   dash-prod

this is my modal code

function get_all_access_list(){
    $db = $this->connect_db();
    $output =array();
    $db->select('access_module_id,access_parent_module,access_module_name');
    $db->from('access_permissions');
    $db->where('access_parent_module =',0);
    $query = $db->get();
    if( $query->num_rows() > 0 ){
        $output = $query->result();
    }
    return $output;
}

this is in view code

<?php 
if( !empty($access_list) ){
    foreach( $access_list AS $acces_name ){ ?>
        <input type="checkbox" name="access_name[]" id="acces_name" value="<?php echo $acces_name->access_module_id; ?>">
        <span class="label-text fa-lg"><?php echo $acces_name->access_module_name;?></span>
<?php 
    }
}
?>

In terms of MySQL query, we can define a custom sorting parameter. If the pid value is 0, we can simply use the id value to sort upon; else the pid value. This is because the pid value for other levels is same as the id for the parent.

We will also use multiple level Order By, with second level ordering done using id.

You can do something like this:

SELECT 
  t.id, 
  t.name 
FROM 
  your_table AS t 
ORDER BY 
  CASE WHEN t.pid = 0 THEN t.id 
       ELSE t.pid 
  END
  , t.id