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

时间:2017-01-09 14:23:10

标签: codeigniter codeigniter-2 codeigniter-3

我在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']的值而不是查看???

5 个答案:

答案 0 :(得分:0)

您在变量中调用模型: -

self.kwargs['page_slug']

因此,您将获得从数据库中获取的结果。比将此变量数据传递给View。

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

因此,您已经拥有了从模型中获取的控制器中的值,该模型存储在$ data中。

您可以使用

查看$ data的值
$this->load->view('home', $data);

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

答案 1 :(得分:0)

试试这个......

在控制器......

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
 }

答案 2 :(得分:0)

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

$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形式提供。

答案 3 :(得分:0)

控制器:

Get-ADUser -Property * -Filter * | Select-Object @{n='Member Of';e={
  (Get-ADPrincipalGroupMembership -Identity $_.SamAccountName |
    Select-Object -Expand Name) -join ', '
}}

模型

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

希望这是你想要的。

答案 4 :(得分:0)

要这样做...您必须将数据提取到模型中 result_array 中的 row_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>