【问题标题】:How to get the value from model to controller如何从模型获取值到控制器
【发布时间】:2015-06-05 10:02:43
【问题描述】:

型号

这是我的模型。使用最后一个查询我在模型中获得了值,但我没有在控制器中获得值

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

【问题讨论】:

  • 你确定你field名字是emailtable名字是change_password
  • 您是提取所有电子邮件还是只提取一封?你试过print_r($dbemail)???
  • 是的,表名和字段名是正确的
  • @Arthi 您需要按照回答更新您的代码。
  • 请更新你的问题你真正想要什么??@Arthi

标签: php codeigniter model controller


【解决方案1】:

CI 是一个 MVC 框架。因此,您需要从控制器发出命令并从模型中获取数据,并且您需要将它们传递给查看。这是最佳实践

控制器

function checking()
 {
 $email=$this->input->post('email');
 $this->load->model('login_model');
 $data['dbemail']=$this->login_model->email();// assign your value to CI variable  
 $this->load->view('home', $data); //passing your value to view
 }

型号

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

查看

额外知识

  1. 视图将在View 文件夹/ Ex im 使用视图名称下创建 作为home.php
  2. 您可以使用自己的样式(它也可以创建为普通的 html 页面。)

     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'] 
        }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2016-02-12
    • 2015-03-11
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多