[eluser]ROFLwtfBBQ?[/eluser]
I'm not sure how I would solve this with "one query" as people are suggesting. However, I moved the query into the model now, but I'm not entirly convinced about the benefits of it. It seems like alot of extra code, and a waste of performance having to first sort the results into the data array in the model, then iterate through the array in the view. But i guess that's an extra neccesary step to keep the business and presentation logic apart.
In the model:
Code:
$query = $this->db->get('categories');
foreach ($query->result() as $category)
{
$anotherQuery = $this->db->get_where('subcategory', array('category_id' => $category->id));
foreach ($anotherQuery->result() as $subcategory)
{
$data['categories'][$category->title][$subcategory->id]['title'] = $subcategory->title;
$data['categories'][$category->title][$subcategory->id]['body'] = $subcategory->body;
}
}
And in the view
Code:
<?php foreach ($categories as $category => $sub):?>
<h2><?php echo $category;?></h2>
<?php foreach ($sub as $subcategories => $subcategory):?>
<h3><?php echo $subcategory['title']?></h3>
<p><?php echo $subcategory['body']?></p
<?php endforeach;?>
<?php endforeach;?>