Welcome Guest, Not a member yet? Register   Sign In
Is it proper coding procedure to have any logic in the model
#1

[eluser]MyDarkPassenger[/eluser]
I was curious whether it was proper procedure to have any logic in the view. All the examples, always show the controller controlling logic and passing the data to the view which render it. However, when you have controllers with different functions they sometimes reuse the same views, like the login prompt. Should I handle this through a template or is it acceptable to have any logic in a view? Also what to you do if records are in say a table? You need to use an open table tag and then dump records in and close it. I don't know how you can do this with either have the controller echo the table tags or have the view loop through records. I'm just wondering about proper procedure.
#2

[eluser]Ben Edmunds[/eluser]
It is fine to have view logic in your views. So looping through results and echoing variables is normal.

You don't want any business or complex logic in your views.

So for examples if you are querying the users from your database and displaying them in a table your controller would call the model, which would return your users, the controller would store the users in an array like

controller
Code:
$this->load->model('users_model');
$this->data['users'] = $this->users_model->get_active_users();


and lastly the view would display the users

view
Code:
<table>
   <tr><td>First Name</td><td>Last Name</td></tr>
   &lt;?php foreach ($users as $user):?&gt;
      <tr><td>&lt;?php echo $user->first_name;?&gt;</td><td>&lt;?php echo $user->last_name;?&gt;</td></tr>
   &lt;?php endforeach;?&gt;
</table>
#3

[eluser]MyDarkPassenger[/eluser]
Thank you that helped. I'll do that anymore I wasn't sure how to handle it.




Theme © iAndrew 2016 - Forum software by © MyBB