[eluser]Philo01[/eluser]
Hi all
I was wonder what the best practice is to do the following.
We got 2 tables, categories and posts like so:
Categories
id
name
Posts
id
category_id
title
content
Now at the moment I often end up running models that run the query's in the view files to get something organized.
So to give you a better example I've replaced the models with the actual query's that are being executed by these models.
Code:
<?php
foreach ($categories->result() as $c)
{
echo '<div id="cat_'.$c->id.'"> <h2>' . $c->name . '</h2>';
$this->db->where('category_id' , $c->id);
$posts = $this->db->get('posts');
foreach ($posts->result() as $p)
{
echo '<p>'.$p->title.'</p>';
}
echo '</div>';
}
?>
This works, but I'm sure this is not the way.
One other thing I did is joining the tables, but was unable to create the same markup as above.
Can someone point me in the good direction! =)
Thanks