[eluser]Chris Newton[/eluser]
This is how I ended up solving it; (view & controller code is super basic for testing purposes)
controller:
Code:
function index()
{
$this->load->model('news', '', TRUE);
$data['news']=$this->news->readNews();
$this->load->view('welcome_message',$data);
}
model:
Code:
function readNews(){
$this->db->select('*');
$this->db->from('news');
$this->db->orderby('id','desc');
$news_info=$this->db->get();
$my_news=array();
foreach ($news_info->result() AS $key =>$value){
$my_news[$key]['news']=$value;
$this->db->select('name, id');
$this->db->from('news_to_categories');
$this->db->join('news_categories', 'news_to_categories.category_id=news_categories.id');
$this->db->where('news_id',$value->id);
$query2=$this->db->get();
$my_news[$key]['categories']=array();
foreach($query2->result() AS $cats){
$my_news[$key]['categories'][]=$cats;
}
}
return $my_news;
}
Doesn't seem ideal, but It works. I keep trying to come at it from different angles, and nothing better comes to mind. I suppose I could (maybe should?) create 2 functions in the model, one to read the news items, and the other to read the categories, and then join them in the controller. Thoughts?
view:
Code:
foreach ($news AS $key=>$item){
foreach ($item['categories'] AS $key2=>$cat){
echo $cat->name;
echo $cat->id;
echo br(1);
}
echo br(2);
echo "subject ".$item['news']->subject;
echo br(2);
echo "date ".$item['news']->date;
echo br(2);
}