1

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
  • Did you try debugging? Try var_dump()ing what's in the $output variable. Commented Mar 19, 2012 at 19:27

2 Answers 2

2

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']; 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's true, that's the reason I was looking for another solution, because the first one wasn't following the MVC rules. Thank you for you answer, it is what I was trying to do. It helped me a lot.
Actually you could pass the MyModel instance directly to the view. And then view gets data from model. Otherwise what you have is actually getting close to MVP instead. Also .. you do not need the check count(). Thats kinda pointless. Anyway, +1 for the effort.
1

You should be able to do this:

foreach($output as $row) {
   echo $row['email'];
}

1 Comment

That's right, it's working now... I guess I don't have enough knowledge, that's why I didn't think of that. Thank you very much!

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.