I use codeigniter for web development. As I am learning it more, I am finding some more issues that I cannot get a proper logical implementation.
I have a article_controller.php and a view article.php . I am trying to display all of the article titles on the page. (This code is just for testing purpose).
Lets say I have another table named images which contains all the images used in the article. So can I use the images_m.php model on the article.php view.
Article_Controller.php
$data['articles'] = $this->article_m->get(); //gets all of the articles
$this->load->view('articles',$data);
Article.php
foreach($articles as $article):
echo $article->title;
$images = $this->images_m->get_by(array('article_id'=>$article->id)); //gets all the images for current article
foreach(images as $image):
echo "<img src='./uploads/".$image->filename ."'/>";
endforeach;
endforeach;
The code works perfectly fine. I have used similar code in a number of websites.
But the main issue is, I have read that it is not a good idea to use models in views. Use models in controllers instead.
So how can I fetch the images for a particular article in a controller.