我使用 codeigniter 进行 Web 开发。随着我对它的了解越来越多,我发现了更多我无法获得正确逻辑实现的问题。
我有一个article_controller.php
和一个观点article.php
。我正在尝试在页面上显示所有文章标题。(此代码仅用于测试目的)。
假设我有另一个名为的表images
,其中包含文章中使用的所有图像。那么我可以在视图上使用images_m.php
模型吗?article.php
Article_Controller.php
$data['articles'] = $this->article_m->get(); //gets all of the articles
$this->load->view('articles',$data);
文章.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.