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 6786201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:13:02+00:00 2026-05-26T17:13:02+00:00

I need to loop through categories and then the top stores within each of

  • 0

I need to loop through categories and then the top stores within each of the categories, and being relatively new to MVC I’ve unsure as to how to accomplish this in keeping with the MVC principles.

At the moment I was planning on doing something like this, but now I look at it I see an awful lot of stuff which I feel should be in the model:

My Controller looks like this:

public function category_list() {

  foreach ($this->CategoryModel->getCategoryList() as $cat) {           

    $data['cat_title'] = $cat['category_title'];        
    $data['list']['stores'] = $this->StoresModel->getStoresByCategory($cat['category_id']); 
    $this->_Load->view('stores_by_category.tpl', $data);

  }

}

Is this the right way around achieving this task or is my feeling of guilt correct?

Many thanks

  • 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-26T17:13:03+00:00Added an answer on May 26, 2026 at 5:13 pm

    The good:

    You are using the controller to populate the data to be used in the view.
    You are making a model call to getCategoryList() vs. native sql, etc.

    The bad:

    Wrapping the template call within the loop. You should localize the the rows from getCategoryList() into $data like your doing, but then call the template once, with the $data array, then your template will iterate through $data.

    Pointers:

    Your controller should be as light as possible, however there is some wiggle room here. If you have business logic which dictates which categories to return, you need to determine if the logic will be accessed from multiple code points. If this is the case, I recommend you put the business logic into a centralized location (new class), which will be responsible for pulling in the model data, massaging it according to business rules, then returning the data to the calling code. If it’s a one time business logic rule, then it is safe to put within the controller.

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

Sidebar

Related Questions

I need to loop through some elements in the page and then, for each
I need to loop through an array and for each element create a textfield.
I need to loop through all table elements, somewhere keeping an index and then
My controller is passing through a list which I then need to loop through
I have a table of urls which I need to loop through, download each
I have a list of words, in Python I need to loop through each
I need to loop through all the matches in say the following string: <a
I need to loop through a set of 2-dimensional array of hidden input fields
i have the following code ..i need to loop through end of the file
Here's what I need to do: 1) Loop through every cell in a worksheet

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.