0

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?

Thanks!

0

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();

Then pass $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;
}

If the result is an array of arrays instead of an array of objects then you can use as follows

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

Update :

  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.

Reference: Model View Controller(MVC) in PHP and This.

10 Comments

WRONG ! Controller should contain neither business not presentation logic.
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, 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.
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.
@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.
|
1
    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.

1 Comment

Thanks. I was hoping this would be a suggested answer.
1

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.

6 Comments

Since I'm using CodeIgniter, I answered as described here 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);.
May be you can clarify me if I'm misunderstanding something so I cal make myself clear, thanks.
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 domain objects( which would be just a part of well implemented model layer ) and storage logic.
@SheikhHeera You will find that most "MVC" discussions have little relevance to CodeIgniter. CI does not 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, Thanks but can you provide any online reference about proper MVC ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.