I am learning code Igniter and am having a hard time following the MVC pattern. Below is the scenario.
MODEL: article
public function get_all_ideas(){
$this->db->select('article_title, article_content');
$query = $this->db->get('article');
return $query;
}
Here, I have created a function to extract all the articles from the table.
CONTROLLER:
public function articles(){
$this->load->model('article');
$data['articles'] = $this->article->get_articles();
$this->load->view('all_article', $data);
}
Here, I have created a function in controller which loads the model and passes the query results to the view file, 'all_article'.
VIEW: all_article
<html....
<?php
foreach($articles->results() as $article){
?>
<div id="box">
<?php echo $article->article_content;?>
</div>
<?php
}
?>
....</html>
I think I have followed MVC pattern in a proper way in the above code. View file will now display all the content of the articles. I have set the css of 'box'
<div id="box">
in such a way that they are kind of small preview box. Also, I have found that there is a text helper, word_limiter() in CI which limits the words. Since these boxes only show preview of article, I planned on using word_limiter(). Now here's the problem.
If I were to limit the word. Then I will be loading a helper class in the SAME VIEW FILE like this
VIEW:
<div id="box">
<?php
$this->load->helper('text');
echo word_limiter($article->article_content, 40);
?>
</div>
You might have noticed that I am loading the helper class in the VIEW file, which is quite not right according to the principle of MVC. Since such logic has to be in CONTROLLER. But since, the method word_limiter() has to be called in each loop, I didn't see a way to fit this into the CONTROLLER. And I know, doing this violates the principle of MVC.
How should it be done? need help