Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:42:06+00:00 2026-05-25T18:42:06+00:00

Something that has always bothered me is doing more than one loop to manipulate

  • 0

Something that has always bothered me is doing more than one loop to manipulate an array.

What I mean is, in the controller the data is fetched from the DB via a model. Lets say we are showing a list of users, and each user has a status (1,2,3 equates to verified, unverified, banned respectively). Within each iteration of the loop the status would be checked and displayed via another Db query (forget mysql joins in this example).

Now, would you do that in the controller within a loop, and then perform another loop in the view with all the data already fetched and pre-formed ready for display (therefore resulting in 2 loops).

–OR–

Would you just do it in the view therefore resulting in the one loop but with model calls from the view. I understand that this is ok in the strict MVC pattern but its frowned upon generally.

It seems silly to loop twice but then its tidier as all the data manipulation is kept within the controller.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:42:06+00:00Added an answer on May 25, 2026 at 6:42 pm

    I would do that nor in the view or the controller but on the model.

    I explain :

    • Your controller’s job is to retrieve the expected user list, check ACL, etc…
    • Your view’s job is to present this data in an elegant form
    • Your Model job’s in to fetch/store data from Database and ensure integrity. Userstatus is a model too for me.

    My configuration make this pretty easy, I use mustache (Php port) for view, which allow me to call methods from my models directly in view. I wrote my own ORM for my models, that way I have wrappers.

    Such code would look like that for me :

    // Controller
    $template = new Template('pages/users.html');
    $template->users = mUser::find(); // return array of mUsers instances
    echo $template->render();
    
    // View
    {{#users}} <!-- For each user -->
      {{getName}} has status {{#getStatus}}{{getStatusName}}{{/getStatus}}<br />
      <!-- getStatus is a method from mUser model, that return a mUserStatus instance -->
    {{/users}}
    
    /* More explain on the view syntax
    {{name}} = $user->getName() (return string)
    {{getStatus}} = $user->getStatus() (return instance of mUserStatus);
    {{statusName}} = $user->getStatus()->getStatusName();
    */
    

    You may want to have request caching for each model instances in request level so that you never runs a request twice times if not needed.

    That seems more natural to me than to delegate it to controller. I try to put business intelligence on controllers, there is no need for intelligence nor programmer intervention to retrieve a status name for each user.

    I Hope it help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that has always bothered me. Wouldnt it make more sense to
One of the things that has always bothered me about <jsp:include..> is that it's
This is something that has always bothered me about PHP and I have never
Something that has always bugged me is how unpredictable the setTimeout() method in Javascript
i have a stored procedure that has to retrieve data from multiple tables something
I have one number that has something related to the currentdate : 634101448539930000 634101448627430000
I've been using JSF for a while but there's something that has always confused
I have found that Scala always has a natural explanation to anything. Always something
Something that has been troubling me for a while: The current wisdom is that
Something that has piqued my interest is Objective-C's BOOL type definition. Why is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.