简体   繁体   中英

How to make a controller in this sample model?

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();
    }
}

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.

您可以将此User_model重命名为User_model并在类User创建新类User您可以通过这种方式调用类User_model您可以从控制器调用模型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM