[eluser]PhilTem[/eluser]
Either, you do it with adjacency list or with nested sets. The queries for nested sets can be found when you google for 'joe celko nested set'. For adjacency lists it's easier.
Code:
function get_categories($parent_id = '0')
{
$this->db
->select('*')
->from('your_categories_table')
->order_by('category_name') // or whatever else ordering you desire
->where('parent_id', $parent_id);
$query = $this->db->get();
$categories = array();
foreach ( $query->result() as $row )
{
// We'll make use of a temporary variable
$tmp = $row;
// Grab the children with recursive function-call
$tmp['children'] = $this->get_categories($row->parent_id);
// Assign it to the return
$categories[] = $tmp;
}
return $categories;
}
Assuming you have a table with a column
parent_id which defaults to
0 for categories and holds the ID of the parent-category for each sub-category.
If you don't mind learning nested sets, then have a look at them
But it's a lot of stuff to understand and the queries are way more difficult than for adjacency lists - though they run much faster