2017-07-14 16 views
0

Codeigniterのビューからモデルにアクセスする方法。CodeIgniterのビューからモデルにアクセスする方法

私は機能を持つモデルを持って、私はUser_controller負荷 ユーザーモデルの$ this - >ロード - >モデル(「Users_model」)に例えばビュー

+0

その後、仮定何コントローラーを行うには? –

+1

https://www.codeigniter.com/user_guide/general/models.html#loading-a-model – user4419336

+0

これまでに試したことを追加できますか? –

答えて

-1

からこの関数を呼び出す必要があります。

次に、ユーザービューページ$ viewpage = this-> Users_model-> somefunction($ id);

+0

あなたの提案された答えは 'MVC'構造化フレームワークの良い習慣ではありません。 –

1

まずCIのマニュアルをお読みください:

をあなたはMVC構造化フレームワークを使用している場合、あなたはそれが動作する方法を次のようする必要があります。お使いのモデルの中に

public function your_controller_function(){ 

    // To load model 
    $this->load->model ('your_model'); 

    //To Call model function form controller 
    $data = $this->your_model->model_function($ex_data); 

    //TO Send data to view 
    $this->load->view('your_view',['data'=>$data]); 
} 

あなたはコントローラーが必要あなたの視野内

public function model_function($ex_data){ 
    // Your Querys 
    // user return to send you query result to the controller anything 
    // Sample Example of query and return 

    $query = $this->db->select('*')  
    ->where('your_column',$data['column_name']) 
    ->get('your_table'); 

    $your_result = $query->row_array(); 

    if($this->db->affected_rows() > 0){ 
     return your_result; 

    } else { 

     return FALSE; 
    } 
} 
0

をあなたが書くことができます。

<?php 
$this->load->model('your_model'); 
$this->your_model->some_function(); 
?>