如何从模型中获取值并在控制器中显示

how to get values from model and display in controller

我正在用 codeigniter 创建一个站点,这是我的控制器、模型和视图:

控制器:

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

查看:

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

但我想在我的控制器中获取 $new_dbemail['name'],我如何在控制器中获取 $new_dbemail['name'] 的值比视图 ???

您在变量中调用模型 :-

$data['dbemail']=$this->login_model->email();

所以在这里你有你从数据库中获取的结果。比您将此可变数据传递给您的视图。

$this->load->view('home', $data);

所以你已经在控制器中有了你从存储在 $data 中的模型中获取的值。

您可以使用

查看 $data 的值
echo "<pre>";
print_r($data); 

它会显示您正在接收的数据

这样试试...

在控制器中...

function checking()
 {
 $email=$this->input->post('email');
 $this->load->model('login_model');
 $dbemail=$this->login_model->email();//array containing all emails
 //For each loop here for displaying emails
 foreach($dbemail as $email){
   $mail = $email; //assigning to variable
   echo $mail."<br/>";//displaying email
   }

 $data['dbemail'] = $dbemail;//for passing to view
 $this->load->view('home', $data); //passing your value to view
 }

首先您需要从模型中的数据库中获取名称。更改查询行:

$query = $this->db->query("SELECT name, email FROM change_password");

然后要在控制器中使用名称数据,您可以试试这个:

function checking()
{
   $email=$this->input->post('email');
   $this->load->model('login_model');
   $data['dbemail']=$this->login_model->email();
   $name = $data['dbemail']['name']; // example usage to retrieve name in controller
   $this->load->view('home', $data); 
}

现在名称数据在控制器中作为 $name 可用。

控制器:

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

型号

public function email()
{
$this->db->select("email"); // the row of the table you want to select.
$this->db->get('change_password'); //table name. you can still use your query, its just much neat when you use a specific query builder. 
$result = $this->result(); //giving you a result of type object
return $result;
} 

查看

foreach ( $dbemails as $dbemail )
 {
  echo $dbemail->email; // the '->' will be the identifier of which row you want to get or display, i assume its email beacause you specify it in the model. 
} 

希望这是您要找的。

为此...您必须将数据提取到 row_array 而不是 result_array在模型中。

控制器

function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();  
$database_field = $data['dbemail']['database_field']; //to get result in controller
$date['database_field'] = $data['dbemail']['database_field']; //to get result in view
$this->load->view('home', $data);
}

型号

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

查看

<p> Database Field value : <?=$database_field;?></p>