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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:10:52+00:00 2026-05-31T13:10:52+00:00

I consider myself as a php beginner, so it may be possible that this

  • 0

I consider myself as a php beginner, so it may be possible that this question is too easy for someone, but I got really confused on how to solve it. I am trying to loop something from the database in my views. So, in a quick way I solved it like this:

I’ve created a function in my model that does the loop and in the same time is creating the html and saves it in a variable. Then, I get that variable from my controller and I pass it in my view. But, it seems that this is not a good way to solve it, since if I want to change my html I need to enter my model function instead some of the view files.

Then, I’ve created another function in my model that looks like this:

function displayUsers() {
$sql = $this->pdo->prepare('select * from user');
$sql->execute();
while($row = $sql->fetch())
    $results[] = $row;
return $results;
}

Now… I take the result in my controller, and send it in the view, but then… I don’t know how to extract the results from my variable. I have done something like this:

while($output) {
    foreach($output[$i] as $key => $value)
        $data[$key] = $value;
    echo $data['email'];
    $i++;
}

But then, in the end it says to me undefined offset, which means I am referring to an array key that doesn’t exist. Can anyone help me on how to solve this issue?

  • 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-31T13:10:53+00:00Added an answer on May 31, 2026 at 1:10 pm

    Proper MVC shouldn’t have any output in the model or the controller.

    Ideally you would have a model that just gets the raw data and returns it in the controller. The controller can then build up an array of values that we’ll call data. For example:

    Controller

    $data['users'] = $this->MyModel->getusers(); // Getting the users from your model
    $data['posts'] = $this->MyModel->getposts(); // Getting the posts from your model
    $this->getTemplate('userdisplay', $data); // Get the template userdisplay and pass data
    

    This gets the data from the model, and then assigns it to a key within the “data” variable. You can then pass the data variable into the template. You’ll then have two variables to work with in the template, $users and $posts.

    You’ll need a “getTemplate” function that properly maps the data array to individual variables for use in the template, but all of the display should be located in the template.

    To answer your specific question at the end, something like this should work in the template:

    if (count($users) > 0) {
        foreach ($users as $person) {
            echo $person['email']; 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I don't consider myself a novice at MySQL but this one has me
I'd consider myself a reasonable standard CSS/XHTML chap but I'm pretty baffled by this.
I consider myself still pretty new to the TDD scene. But find that no
I consider myself a very beginner at python(and programming in general!), but I am
I consider myself pretty good with Regular Expressions, but this one is appearing to
I consider myself quite fluent in PHP and am rather familiar with nearly all
So I would consider myself a .Net and ASP.NET pro but I am a
I consider myself a very experienced SQL person. But I'm failing to do these
I've been developing for quite a while but while I consider myself competent (or
to start I consider myself in between beginner and intermediate when it comes to

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.