2017-01-09 59 views
0

從模型和顯示值我提出在笨站點,這裏是我的控制器,模型和視圖:如何讓控制器

控制器:

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']的值而不是視圖?

回答

0

您呼叫的模式在一個變量: -

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

所以在這你有你是從數據庫中獲取結果。比你傳遞這個變量數據到你的視圖。

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

因此,您已經在控制器中獲得了從存儲在$ data中的模型中獲取的值。

可以查看$數據中的值,使用

echo "<pre>"; 
print_r($data); 

,它會告訴你正在接受

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 
} 
+1

這是一個奇怪的代碼,你不只是從控制器回聲的東西! – jtheman

+0

@jtheman他只選擇電子郵件並希望在控制器中獲得電子郵件值。 –

+0

不,這不是他要問的。 「但我想在我的控制器中獲得$ new_dbemail ['name'],我如何在控制器中獲得$ new_dbemail ['name']的值而不是視圖???」 - 仍然 - 您不在CI中的控制器中回顯(),您使用視圖輸出! – jtheman

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

0

控制器:

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

希望這你想找的。