简体   繁体   English

如何在此样本模型中制作控制器?

[英]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. 抱歉,我只是使用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. Kinda对控制器感到困惑,因为我知道模型会将查询保留在数据库中。 I dont know how to pass or work on controller. 我不知道如何通过或在控制器上工作。

Am I doing it right way in Model and Controller? 我在Model和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. 而且我不使用任何框架,而不仅仅是php本身和mvc模式。

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: 如果将while循环(来自上一个问题的代码)传递给select()方法:

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您可以从控制器调用模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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