如何从模型到控制器获取值

时间:2015-06-05 10:02:43

标签: php codeigniter model controller

模型

这是我的模型。使用上次查询我在模型中获得了价值,但我没有在控制器中获得价值

class Login_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
    public function email()
    {
        $this->db->select('email');
        $this->db->from('change_password');
        $result=$this->db->get();
        return $result;
    }
}

控制器

class Login extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }

    function checking()
    {
        $email=$this->input->post('email');
        $this->load->model('login_model');
        $dbemail=$this->login_model->email();
        echo $dbemail;
    }
}

1 个答案:

答案 0 :(得分:0)

CI是 MVC 框架。因此,您需要从Controller获取命令并从模型中获取数据,并且您需要将它们传递给视图。这是一个最佳实践

<强>控制器

public function email()
 {
   $query = $this->db->query("SELECT email FROM change_password");
   $result = $query->result_array();
   return $result;
 }

<强>模型

View

查看

额外知识

  1. 视图将使用视图名称在home.php文件夹/ Ex im下创建 如 foreach ( $dbemail as $new_dbemail ) { echo $new_dbemail['database_field'];//in here you can get your table header. //Ex if your table has name field and you need to sho it you can use $new_dbemail['name'] }
  2. 您可以使用自己的样式(也可以创建为普通的html页面。)

    {{1}}
相关问题