2016-12-31 22 views
1

私は自分のウェブサイトの検索ボックスを実装しようとしています。私のユーザーアカウントテーブルでは、私は3つのアカウント、すなわち住宅所有者、管理者、および非アクティブ化されたユーザーに分けました。私は検索機能が複数の列(ユーザー名、名、姓、住所、姓と名の組み合わせ)で検索できるようにしたい。しかし、私のモデルでは、WHERE節は無視されているようです。私のsearch_homeowner関数では、住宅所有者のみを表示するように結果を制限しますが、管理者の結果を表示します。私は複数のLIKEを持つことができないので、ORを試してみました。ここにコードがあります。複数の類似句Codeigniter

モデル:

function search_homeowner($searchquery) { 

    $this->db->select('*')->from('accounts')->where('role', 0)-> where('isActive', 1); 
    $this->db->like('firstname',$searchquery,'after'); 
    $this->db->or_like('lastname',$searchquery,'after'); 
    $this->db->or_like('username',$searchquery,'after'); 
    $this->db->or_like('address',$searchquery,'after'); 

    $query = $this->db->get(); 
    if($query->num_rows() > 0) { 
     return $query->result(); 
    } else { 
     return $query->result(); 
    } 
} 

コントローラー:

function search_homeowner() { 
    $this->load->model('model_accounts'); 
    $searchquery = $this->input->post('search'); 

    if(isset($searchquery) and !empty($searchquery)) { 
     $data['users'] = $this->model_accounts->search_homeowner($searchquery); 
     $data['main_content'] = 'view_adminaccounts'; 
     $data['homeownerlinks']=''; 
     $this->load->view('includes/admin_accounts_template', $data); 
    } else { 
     redirect('admin_accounts/homeowner'); 
    } 
} 

enter image description here enter image description here

答えて

0
use this query hope it will help for you. 

$whereaccounts=array('role'=>0,'isActive'=>1); 
$this->db->select('*')->from('accounts')->where($whereaccounts); 
$this->db->where('firstname', 'LIKE', '%' . $searchquery . '%'); 
$this->db->where('lastname', 'LIKE', '%' . $searchquery . '%'); 
$this->db->where('username', 'LIKE', '%' . $searchquery . '%'); 
$this->db->where('address', 'LIKE', '%' . $searchquery . '%'); 
$query = $this->db->get(); 
+0

のようにあなたの答えのためこんにちは感謝を試してみてください、私は、検索ボタンを押したときますprint_r(の$ this - > DB-> last_query)を保つ – coderszx

+0

、今ではすべてのレコードを表示しません。 $ query = $ this-> db-> get();の下にあります。この特定の行は一度クエリのチェックを取得し、データベース内で一度実行すると、間違いが正確に起こる点が得られます。 – Sona

+0

これで、 'SELECT * FROM 'accounts' WHERE 'role' = 0 AND 'isActive' = 1 AND 'firstname' = 'LIKE' AND 'lastname' = 'LIKE' AND 'username' = 'LIKE' AND 'address' = 'LIKE' '$ searchquery'は読み込まれていないようです – coderszx

0

"%を検索文字列" のプリントである後、使用しているor_likeのCI法。削除した場合、この "%searchstring%"のように表示されます。したがって、この

function search_homeowner($searchquery) { 
     $this->db->select('*')->from('accounts')->where('role', 0)-> where('isActive', 1); 
     $this->db->like('firstname',$searchquery); 
     $this->db->or_like('lastname',$searchquery); 
     $this->db->or_like('username',$searchquery); 
     $this->db->or_like('address',$searchquery); 

     $query = $this->db->get(); 
     if($query->num_rows() > 0) { 
      return $query->result(); 
     } else { 
      return $query->result(); 
     } 
    } 
関連する問題