[eluser]pollo[/eluser]
Hi everyone i´m doing a forum like this one and i have these tables relationships:
Code:
category has many forum
forum belongs to category
forum has many thread
thread belongs to forum
I want to retrieve all
categories and for each category all
forums and for each forum the number of
threads.
This is the query i´m doing:
Code:
$this->db->select('c.title as category_title, f.title, f.description, f.slug, COUNT(t.id) AS 'num_threads');
$this->db->from('category c');
$this->db->join('forum f', 'c.id = f.category_id', 'left');
$this->db->join('thread t', 'f.id = t.forum_id', 'left');
$this->db->group_by('f.id');
$query = $this->db->get();
$query->result();
And it returns something like this:
Code:
Array
(
[0] => Array
(
[category_title] => The CodeIgniter Lounge
[title] => Introduce Yourself!
[description] => Use this forum to introduce yourself to the CodeIgniter community, or to announce your new CI powered site.
[slug] => introduce-yourself
[num_threads] => 2
)
[1] => Array
(
[category_title] => The CodeIgniter Lounge
[title] => The Lounge
[description] => CodeIgniter's social forum where you can discuss anything not related to development. No topics off limits... but be civil.
[slug] => the-lounge
[num_threads] => 0
)
...
)
But what i want is something like this:
Code:
Array
(
[0] => Array
(
[category_title] => The CodeIgniter Lounge
[forums] => Array
(
[0] => Array
(
[title] => Introduce Yourself!
[description] => Use this forum to introduce yourself to the CodeIgniter community, or to announce your new CI powered site.
[slug] => introduce-yourself
[num_threads] => 2
)
[1] => Array
(
[title] => The Lounge
[description] => CodeIgniter's social forum where you can discuss anything not related to development. No topics off limits... but be civil.
[slug] => the-lounge
[num_threads] => 0
)
)
)
...
)
There is a way to do this?