2016-09-07 6 views
1

私はページの内容を動的に表示したいが、このコードはうまくいきますが、特定のIDの内容しか表示されません。CI内のsqlでwhere句を動的にする

モデル

public function getAllDepartment() { 
$join = $this->db->query("Select * from labreport_db inner join patient_db on labreport_db.P_ID=patient_db.id where labreport_db.P_ID=15"); 
return $join->result_array(); 

    } 

コントローラ

public function reportDisplay(){ 

$data['clubs'] = $this->labdoc_model->getAllDepartment(); 
$this->load->view('SystemAdminUser/labreport', $data); 
    } 
+0

通過P_ID = getAllDepartment($ P_ID)15。変数としてモデルで受信し、その変数を動的としてクエリに入れます –

+0

デモ用に15個しか渡したくありません。それは動的に変化します。これは私が知りたいものです。 – oxy

答えて

0

それを行う方法はいくつかあります。そのうちの1つは、CIルーティング能力によるものです。

モデル

public function getAllDepartment($p_id = 0) { 
    $join = $this->db->query("Select * from labreport_db inner join patient_db on labreport_db.P_ID=patient_db.id where labreport_db.P_ID={$p_id}"); 
    return $join->result_array(); 
} 

コメント:私は動的にIDを取得する変数として$p_idを追加しました。

コントローラ

public function reportDisplay($p_id = 0){ 
    $data['clubs'] = $this->labdoc_model->getAllDepartment($p_id); 
    $this->load->view('SystemAdminUser/labreport', $data); 
} 

コメント:我々はまた、reportDisplay機能に$p_idという変数を追加し、モデルのgetAllDepartment()関数に渡します。レポートを取得する方法

動的

私はあなたのURLの構造を知らないが、例えば目的は、のは、それが単にreportDisplay

後にIDを追加し、それを動的にアクセスするには http://localhost/yourcontrollername/reportDisplay/

を言うてみましょう例えば

1

は、のようないくつかの変数にIDをホールド:

$pid = $_REQUEST['p_id']; 
// $_REQUEST['p_id'] will contain the dynamic value in it 

などクエリでこの変数を置く:

where labreport_db.P_ID = $pid; 

$ pidに含まれる値のデータが表示され、その中に動的な値が含まれていることを確認します。

+0

私はその値を動的にします。 15の代わりに他の値を指定することもできます。 – oxy

+0

更新された回答を確認してください –

1

これは、CIのQuery Builderクラスを使用して簡単に回避できます。

だから、これは

$id=15; 
$this->db->select('*'); 
$this->db->from('labreport_db'); 
$this->db->join('patient_db', 'labreport_db.P_ID = patient_db.id'); 
$this->db->where('labreport_db.P_ID', $id); 
$query = $this->db->get(); 

、あなたのコードは次のようになります方法ですこれは、あなたがダイナミックWHERE条件を実行可能なクエリビルダクラスを使用してデータベース操作を行うためのCIでの標準的なアプローチです。

$idの値を変更するだけで、クエリが必要になります。

参考:https://www.codeigniter.com/userguide3/database/query_builder.html#selecting-data

1

あなたは解決策のために、このコードを使用することができます。

Model.php

public function getAllDepartment($pId) { 
    $join = $this->db->query("Select * from labreport_db inner join  patient_db on labreport_db.P_ID=patient_db.id where labreport_db.P_ID=".$pId); 
    return $join->result_array(); 
} 

Controller.php

public function reportDisplay(){ 
    $pid = $_REQUEST['pid']; //OR $_GET['pid']; OR $_POST['pid']; You can pass your id for get content 
    $data['clubs'] = $this->labdoc_model->getAllDepartment($pid); 
    $this->load->view('SystemAdminUser/labreport', $data); 
} 
+0

これは同じです... id 15のコンテンツが表示されます。それは変化する動的でなければならない。 – oxy

+0

あなたはcontorllerからIDを渡すことができ、出力を得ることができます。 reportDisplayアクションは 'POSTとGET'を介して取得します' getAllDepartment'関数でパスIDを要求します –