I want use right MVC concept. So, say in model we have for example query to DB
class my_model {
public function getUserData () {
$res = $this->db->query("
SELECT name, lastname, age, mail FROM users
");
return $res;
}
}
What I do: I pass mysqli_result object to view and after in view in while cycle I print data from DB.
But as I know, pass mysqli_result to view, isn’t right MVC.
So, can you show me, what is right MVC solution, in situations like this ?
In MVC, the view would request the data from the model directly and would not be passed data by anything. The view has a dependency on the model and requests its own data. See this answer: How is MVC supposed to work in CodeIgniter for a more detailed description on why this is so.
The answer is to initialise the view after the model and pass the view the entire model in the constructor.
e.g.