Welcome Guest, Not a member yet? Register   Sign In
Controller vs View
#1

[eluser]Buso[/eluser]
Hi guys, I was building a blog with codeIgniter but I don't know enough about how I should distribute the responsibilities.

Where do you think I should process the query results?? For example, if I get 1 article as result, should I do something like this in the controller?:



// PAGE DATA
$row = array_shift($this->blogmodel->getLastArticle()); // *
$article_data['title'] = $row['title'];
$article_data['body'] = $row['body'];
[...]
// PAGE STRUCTURE
$data['header'] = ...
$data['content'] = $this->load->view('blog/article',$article_data,true)
$data['footer'] = ...
[...]
// PUT ALL TOGETHER INTO THE LAYOUT
$this->load->view('layout',$data);


*Maybe I could use $article_data instead of $row and avoid the assignations, whatever


It was pretty clean till I had to do the same with the comments, where there could be 50 of them or more, each one with its respective html tags (no point of doing that in the Controller).

But I wanted to be consistent, so.. maybe you could gimme some advices about this, or link me to something to read??

Thanks in advance ^^
#2

[eluser]bigtony[/eluser]
The view is the place to put all of your "view logic". So when you retrieve your row, you can pass it to the view and the view can access it like this:
Code:
<h1>&lt;?php echo $row->article_heading; ?&gt;</h1>
<p>&lt;?php echo $row->article_content; ?&gt;</p>

For comments, you can pass in the returned result set (e.g. 'comments') and use like this:
Code:
&lt;?php foreach ($comments as $a_comment): ?&gt;
    <h2>&lt;?php echo $a_comment->author; ?&gt;</h2>
    <p>&lt;?php echo $a_comment->message; ?&gt;</p>
&lt;?php endforeach; ?&gt;

It's also acceptable within MVC for the view to make the calls to read the model data instead of the controller, then all you would need to pass to the view is the article id. (But don't use the view to call any model updates - leave that to the controller).




Theme © iAndrew 2016 - Forum software by © MyBB