Search code examples
phpcodeigniteractiverecordsql-order-byhierarchical-data

Parent category not display first in array?


My model function get forums is not showing the correct order or my parent and sub categories.

The parent category PHP should be first at top of array because it is THE parent of category 46, 45

Question: How can I get it to make sure it displays correct order

Array
(
    [0] => Array
        (
            [fid] => 46
            [pid] => 41
            [name] => password_hash
        )

    [1] => Array // Parent Category
        (
            [fid] => 41
            [pid] => 0
            [name] => PHP
        )

    [2] => Array
        (
            [fid] => 45
            [pid] => 41
            [name] => str_replace
        )

)

Model

public function get_forums($pid = '0')
{
    $categories_data = array();

    $this->db->where('pid', $pid);
    $this->db->or_where('pid >', $pid);
    $this->db->order_by('name', 'asc');

    $query = $this->db->get('forum');

    foreach ($query->result() as $result) {
        $categories_data[] = array(
            'fid' => $result->fid,
            'pid' => $result->pid,
            'name' => $result->name
        );

        if ($result->pid > 0) {
            $category_children[] = $this->make_parent_list($result->pid);
            if ($category_children) {
                $categories = array_merge($category_children, $categories_data);
            }      
        }
    }     
    return $categories_data;
}

function make_parent_list($fid) {
    $forum_data = array();
    $forum_query = $this->db->query("SELECT * FROM forum WHERE fid = '" . (int)$fid . "'");
    foreach ($forum_query->result() as $forum) {
        if ($forum->pid > 0) {
            $forum_data[] = array(
                'pid' => $forum->pid,
            );

            $forum_children = $this->make_parent_list($forum->pid);
            if ($forum_children) {
                $forum_data = array_merge($forum_children, $forum_data);
            }           
        }
    }

    return $forum_data;
}

Solution

  • The problem is in the ordering. Change this

    $this->db->order_by('name', 'asc');
    

    with something like this:

    $this->db->order_by('fid', 'asc');
    

    You can then order the results by the name too, adding it as secondary:

     $this->db->order_by('fid', 'asc');
     $this->db->order_by('name', 'asc');