[eluser]jedd[/eluser]
Hi Kerran, and welcome to the CI forums.
Quote:I have minimal experience with Codeigniter and I have no problem getting this to work with other frameworks such as doctrine but I am finding it difficult getting it working with CI.
I'd be curious how you get this working in other frameworks - do they have a hierarchical function for presenting this kind of data natively?
There are a few ways of doing what you want.
First, easiest, you could select your parent ID's, and then do a query within a loop based on each of those. This sounds ugly, but two things to consider - queries are cheaper than we often assume they will be (DB programmers tend to put a fair amount of effort into reducing query costs) and secondly, you're likely not going to get all children to all parents in one fell swoop (you'll either be paginating, or you'll be a bit more selective than 'SELECT * FROM ...').
Alternatively you could exercise more care in your model, basically putting that logic back into the PHP on the data as it comes in, massaging it into an extra-dimensioned array, and then returning that new construct to your controller.
Yet another alternative is to handle this entirely within your presentation logic - your view - and that's probably where I'd do it.
Consider your intent:
Quote:I need some support regarding how I can now run a foreach loop that assigns all the children topics to the appropriate parent.
so that output will look like this:
TOPIC A
CHILD A
CHILD B
TOPIC B
CHILD A
TOPIC C
CHILD A
CHILD B
apposed to
TOPIC A CHILD A
TOPIC A CHILD B
TOPIC B CHILD A
TOPIC C CHILD A
TOPIC C CHILD B
That latter set is an accurate reflection of what a LEFT JOIN provides.
Anyhoo, yes, my preference would be to mangle this in the view - it's not a terribly complex algorithm to go through each parent, and if it's different to the previous parent then unindent .. etc etc. Do you need help with the details?
Btw, you may be better off getting an array rather than objects as your return type .. say, using result_array() rather than pulling in each row (an object) and assigning it to an array element. So, instead of this:
Code:
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
.. try this :
Code:
return ($q->num_rows() > 0) ? $q->result_array() : FALSE;