How to make a controller in this model and pass to view? Sorry I'm just new in using mvc oop. I just want to learn the basic. Kinda confused on controller, since I know the model will hold the queries on the database. I dont know how to pass or work on controller.
Am I doing it right way in Model and Controller? I just need some advice. On how to handle Model and Controller correctly.
And I'm not using any framework just php itself and mvc pattern.
Model
class userModel{
public function __construct(){
$dbCon = new DbConnector();
$this->dbCon = $dbCon->getConnection();
}
public function select(){
$myQuery = "SELECT * FROM users;";
$results = $this->dbCon->query($myQuery);
return $results;
}
}
controller
require_once("../model/userModel.php");
class userController{
private $userModelSelector;
public function __construct(){
$this->userModelSelector = new userModel();
}
}
you can rename this calss to User_model
and create new class User
in class User
you can call class User_model
by this way you can call model from controller
If you pass the while
loop (from your previous question's code) to your select()
method:
class userModel{
public function __construct(){
$dbCon = new DbConnector();
$this->dbCon = $dbCon->getConnection();
}
public function select(){
$myQuery = "SELECT * FROM users;";
$results = $this->dbCon->query($myQuery);
// I added this bit
while($getUsers = $results->fetch_array()){
echo $getUsers['username'] . "<br>";
}
return $results;
}
}
and then use that method passed from your controller in the return:
$user = new userModel();
return $user->select();
it will work, given if this is what the question is about.