PHP MVC, ver manejo de bucles

My controller returns a PDO array of several rows and columns. What is the best way of looping through the data in my view file? (or should I be doing more logic in the model?)

$user->getDetails();  //returns array in view


foreach($user->getDetails() as $row)  // Prehaps? But how to index without being explicit with column names?

¡Gracias!

preguntado el 31 de julio de 12 a las 15:07

4 Respuestas

The point of the view is to run view-related logic and output results (i.e. take the data and format it as necessary...if it's a JSON view, output JSON. HTML output HTML, etc.), thus you're doing it right. Loop through it however you see fit, but without more details about your data structure, we can't say more than you already have figured out.

Respondido 31 Jul 12, 15:07

The best way or the approach being used in MVC is to keep your programming logic in the controller, do the database related stuff in the model and format the layout in the view. In your case you should call the $user->getDetails(); in the controller and then pass the result in the view then loop it to output the data, i.e.

In the controller you can populate a variable with $user->getDetails()

$user_details=$user->getDetails();

Entonces pasa $user_details to the view when you load it and in the view loop it

foreach( $user_details as $row ){
   echo $row->id;
   echo $row->name;
}

Si el resultado es un array of arrays en lugar de an array of objects then you can use as follows

foreach( $user_details as $row ){
   echo $row['d'];
   echo $row['name'];
}

Actualización:

  1. The model is responsible to manage the data; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.
  2. The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla, …
  3. The controller handles the model and view layers to work together. The controller receives a request from the client, invoke the model to perform the requested operations and send the data to the View. The view format the data to be presented to the user, in a web application as an html output.

Referencia: Model View Controller(MVC) in PHP y Esta .

respondido 12 nov., 13:17

WRONG ! Controller should contain neither business not presentation logic. - tereško

The best way or the approach being used in MVC is to keep your programming logic in the controller, do the database related stuff in the model (..) <= you said it here. Also, model is not a database abstraction. - tereško

@tereško, I'm confused, can you clarify more what did you mean ? I'm still now saying that controller should contain all the programming logic and from there you should call model functions declared in any model. - El alfa

Thanks for your informed response. However why can I not call the function from within the view? I.e why assign it to a variable and then pass that to the view, is that not unnecessary code? Look forward to your reply. Thanks. - craig wilson

@SheikhHeera Though NOT doing enough research(!) and reading books your answer satisfied the OP, got highest upvotes, it reminds me the ever running battle between a successful business man and a business student. - Muhammad Usman

    foreach( $user->getDetails() as $row ){
       echo $row->name;
       echo $row->id;
    }

You don't need to put the $user->getDetails() at the top, it will call the function in the foreach.

Respondido 31 Jul 12, 15:07

Thanks. I was hoping this would be a suggested answer. - craig wilson

The model layer should contain all the business logic. And it should not return anything to the controller. Instead the controller should just send messages to model layer structures.

The data from model layer should be extracted by view instance. And depending on nature of data, it would decide which templates to apply.

Views in MVC are supposed to contain all the presentation logic and ( in case of web-related MVC-inspired design patterns) deal with multiple templates. You should also be aware that there exists a 1:1 relation between views and controllers.

If if part of information, that view receives from model layer, is some sort of array, you have two choices. Either you take a template, which can render a single item and repeatedly generate the HTML/JSON/text/XML fragment or you use a template , which expect to receive an array as variable and already contains a loop. The latter approach is usually the more pragmatic one, but each of them as a specific cons an pros.

Respondido 31 Jul 12, 17:07

Since I'm using CodeIgniter, I answered as descrito aquí in example. This $data['query'] = $this->Blog->get_last_ten_entries(); is inside a controller and it retriving data from model, which means the modekis returning data to the controller and then the controller passing the data to date view as follws $this->load->view('blog', $data);. - El alfa

May be you can clarify me if I'm misunderstanding something so I cal make myself clear, thanks. - El alfa

Why are you pasting links to one of two works php frameworks in this topic ? What CI call "models" are activerecord instances. Activerecord is a melding of objetos de dominio( which would be just a part of well implemented model layer ) and storage logic. - tereško

@SheikhHeera You will find that most "MVC" discussions have little relevance to CodeIgniter. CI does no implement the MVC pattern (despite whatever the CI wiki says), but rather something closer to MVP (but with templates). Basically, you'll never get CI to fully conform to the traditional MVC pattern. - orourkek

@orourkek, Thanks but can you provide any online reference about proper MVC ? - El alfa

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.