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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:30:43+00:00 2026-05-20T17:30:43+00:00

This may be chalked up to exhaustion, but I am new to MVC programming

  • 0

This may be chalked up to exhaustion, but I am new to MVC programming and while I have some grasp on it, I am really struggling with a concept. I am working in php/codeigniter on a system that allows a user to post entries and other users are able to vote on them. I have stored the entries in a mysql table entries and the votes in another table votes.

The field that links them together would be entries->id and votes->entryId.

So, in my controller, I have a function that calls the model and gets the recent entries:

$data['recentEntries'] = $this->Entries->getRecentEntries($id);

where $id is the id number of the user’s entries you want to get.

I want to be able to display each entry alongside the corresponding votes for each entry, and normally without MVC I would simply find all the entries for a user, then in a foreach loop, find and display all the votes for each particular entryId.

However, with MVC, the entries are loaded into the variable $data[‘recentEntries’] and I wouldn’t or shouldn’t use the foreach loop to query mysql in the view.

I feel like I am missing a critical mysql or MVC concept here that should take care of this quite simply, so any direction or guiding would be incredibly appreciated.

Many thanks in advance.

— Update —

So I’ve updated the controller so:

$data['recentEntries'] = $this->Entries->getRecentEntries($id);

$data['votes'] = array();

foreach ($data['recentEntries'] as $entry) {
    array_push($data['votes'], $this->Votes->getVotes($entry->id));
}

So that $data[‘votes’] now contains the votes for all the entries for a particular user. Is this the right way to go about it? If so, then once in the view’s foreach loop, how do I find and display the appropriate values from this $data[‘votes’] array?

View:

if (count($recentEntries)>0) {
    foreach ($recentEntries as $row)
    {
        echo $row->content;
    }
}
  • 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-20T17:30:44+00:00Added an answer on May 20, 2026 at 5:30 pm

    You probably to maintain it as a map rather than a simple array so that you maintain the association between an entry and it’s votes. And, yes, I think it’s best to populate the entire model in the controller rather than querying the DB from the view.

    foreach ($data['recentEntries'] as $entry) {
         $data['votes'][$entry->id] = $this->Votes->getVotes($entry->id);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a stupid question but I have some code like: $userData =
This may sound like a newbie question, but I really need some help with
This may be a stupid question but I have a code with the following
This may be have a better name than custom tab completion, but here's the
This may be the wrong place but this is new to me. I did
This may sound like a very generic question but here it goes. I have
This may seem simple, but somehow it isn't. I have a table of historical
This may not be possible, but I have a DIV that I am transforming
this may be a simple question but am struggling to understand how to solve
This may seem like a simple problem to solve, but I'm new to Android

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.