Welcome Guest, Not a member yet? Register   Sign In
Clean way to work with views
#1

[eluser]jfong[/eluser]
Hi,

Great framework and I'm really learning fast - just trying to learn how to do this properly. I have a function to get everything from the db in the model, which passes to controller as a $data array. In the controller, I am assigning it as follows:

$data['everything'] = $this->Everything_model->everything();

and passing $data to the view. In the view, I do:

foreach ($everything as $row) {
echo $row['recordname'];
}

I can get everything to show properly, however I feel like I can somehow do all the processing in the controller, then use something like a variable $recordname in the view. Is this possible by assigning a variable in the controller like $recordname = $data['everything']? Or, do I have to keep the data as an array then expand it in the view?

Any help would be appreciated! Thank you!
#2

[eluser]Bart v B[/eluser]
I don't understand your problem.

Normaly it would look like this:

Youre Controller:
Code:
<?php

class Test extends Controller
{
   function __construct()
   {
     parent::Controller();
     $this->load->model('some_model');
   }

   function index()
   {
      $data['everything'] = $this->some_model->GetItems();
      $data['header'] = 'Hello CodeIgniter!';
      $this->load->view('test_view', $data);
   }
}

Youre model should look like this:

Code:
<?php

class Some_model extends Model
{
   function GetItems()
   {
     $query = $this->db->get('mytable');

     if($query->num_rows() > 0)
     {
      foreach ($query->result() as $row)
      {
        $data[] = $row;
      }
       return $data;    
    }
    
   }
}

Your view:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo $header; ?&gt;&lt;/title&gt;


&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?php echo $header;?&gt;</h1>

&lt;?php  if(!empty($everything )) :
       foreach($everything as $item): ?&gt;

   <p>&lt;?php echo $item->some_row; ?&gt;</p>

&lt;?php
      endforeach;
      endif;
?&gt;
&lt;/body&gt;
&lt;/html&gt;

As you see you don't need to have a foreachloop when its an array from the controller.
$data is just an array() to have some infomation of where it's comming from.
Cleander as this is not possible. So i think your problem is something else.
Maybe you can give more information aboute what you can not do in your view.
#3

[eluser]developer10[/eluser]
Actually he wants to know whether it would be better to do a foreach loop in his controller, then assign everything to a variable, then pass it to the view.

I can tell that there's nothing wrong in using loops in view files. We all do that, it's a common practice so keep using it.
#4

[eluser]mi6crazyheart[/eluser]
As u've said... "I can somehow do all the processing in the controller, then use something like a variable $recordname in the view"

I think u'r already doing pretty much every things in u'r controller & using VIEW only for showing data to user. Don't u feel like this!!! If not, why... ?
#5

[eluser]jfong[/eluser]
Thanks for all the help everyone - I think cold_fusion hit it on the head. Wasn't sure if it was bad practice to use the foreach in the view file when I'm basically reusing the information throughout the application and copy/pasting the same code in different views. Really appreciate all the help, this framework rocks!
#6

[eluser]Angel Web Design[/eluser]
As long as its only presentation logic, i.e. foreach loops iterating through data to display, then theres no problem mate. Just stay clear of doing business / data logic inside the view and you're home safe!
#7

[eluser]William Rufino[/eluser]
as everyone above said its fine to do loops inside the views! as long as they are only to display data, no logic on it!
#8

[eluser]tonanbarbarian[/eluser]
in fact i would say you should NOT do any loop processing in the controller IN THIS CASE (in some very specific cases it is ok)
in this example you are simply displaying the values from the data in the view.
so if you were to loop through that data in the controller what would you be doing? probably just creating a string and putting paragraph tags around each item and adding it to the string. you would then send the string to the view and it would echo it out.

Why should you NOT do that in the controller.
simple. MVC is about code separation. You are separating business logic (Controller) from data logic (Model) and presentation logic (View)

So any time you need to present something to the user you should do that in the view.

The idea is that ultimately you can assign the responsibility for each layer to a different specialist.
So you can have a developer doing the business logic (controller), a database designer doing the data logic (model) and a front end designer doing the presentation logic (view). Each person only needs to understand how their part of the process works and they only work on their own files.

So if you put presentation code into the controller the view has no control over it.

Really there are probably very few cases in the controller where you should loop through any data returned by the model.
In some cases you might want the controller to exclude some results returned from the model, but ideally the model should already have excluded the results
The controller determines what data is needed by the view

the model can loop through the data is is generating of course, but it should NOT format the data in any meaningful way.
it might truncate some data that is too long, or concatenate multiple fields, but it should not style or otherwise format the data it generates

this does mean more code in the view, but that is how MVC should work. each controller, model and view should be able to be coded and treated separately and in isolation

so the controller should not know how the model is gathering data or how the view will present it, it just knows what data it needs for what action (method)
the model should not need to know why data is needed or how it is going to be presented, it should just return the data that is asked for
and the view should not care how the data was gathered, it just knows that it has certain data it expects and how it will format it.
#9

[eluser]jfong[/eluser]
Wow, awesome replies, thanks for all the help!




Theme © iAndrew 2016 - Forum software by © MyBB