2017-03-21 6 views
0

私の問題は私が入力するたびにfnamemname私はデータベースからエラーを受け取りました。検索フィールドにfnamemnameの両方を検索したいだけです。フレームワークcodeigniterを使用した検索機能、PHP

searchコントローラ機能

public function search() 
    { 
    $li = $this->session->userdata('logged_in'); 
    $id = $this->session->userdata('idnumber'); 
    if($li == TRUE) 
    { 
     $this->load->model('users_model'); 
     $this->load->helper('smiley'); 
     $this->load->library('table'); 
     $image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status'); 
     $col_array = $this->table->make_columns($image_array, 8); 
     $image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status'); 
     $col_array2 = $this->table->make_columns($image_array2, 8); 
     $this->data['smiley_table1'] = $this->table->generate($col_array); 
     $this->data['smiley_tables'] = $this->table->generate($col_array2);   
     if($this->input->post()) 
     { 
     $search = $this->input->post('search'); 
     $memb = $this->users_model->search($search); 
     $usersearch = $this->users_model->search($search); 
     $grpsearch = $this->users_model->searchgrp($search); 
     redirect ('home/profile/'.$memb); 
     } 
    } 
} 

マイusers_modelモデル

public function search($search) 
{ 
    $this->db->select('*'); 
    $this->db->from('users'); 
    $this->db->like('username',$search); 
    $this->db->or_like('fname',$search); 
    $this->db->or_like('lname',$search); 
    $this->db->or_like('mname',$search); 
    $query = $this->db->get(); 
    foreach ($query->result_array() as $row) 
    { 
     $memb = $row['idnumber']; 
    } 
    $error = 'There is no record for the one you searched. Please go Back.'; 
    $query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'"); 
    $hehe = $query1->result_array(); 
    if($hehe==NULL) 
    { 
    echo $error; exit; 
    } 
    else 
    { 
    return $memb; 
    } 
} 
+0

正確なエラーは投稿できますか? –

+0

お待ちください:) –

+0

私はすでにそれを投稿しました。 –

答えて

1

必ず状態で使用してvariblesを確認し、条件の前に宣言されています。 それはyoullのは、クエリで使用する変数が設定されていることを確認し、出力

 public function search($search) 
     { 
      $this->db->select('*'); 
      $this->db->from('users'); 
      $this->db->like('username',$search); 
      $this->db->or_like('fname',$search); 
      $this->db->or_like('lname',$search); 
      $this->db->or_like('mname',$search); 
      $query = $this->db->get(); 
      $memb = null; 
      foreach ($query->result_array() as $row) 
      { 
       $memb = $row['idnumber']; 
      } 
      $error = 'There is no record for the one you searched. Please go Back.'; 
      $query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb"); 
      $hehe = $query1->result_array(); 
      if($hehe==NULL) 
      { 
      return $error; 
      } 
      else 
      { 
      return $memb; 
      } 
     } 
+0

条件が合格しなかった場合、エラーメッセージが表示されます。 –

+0

したがって、私たちは常に条件の変数を使用するときに両方のシナリオをチェックするべきです –

+0

'fname'と' mname'の両方を入力しようとしましたが、上記エラーは表示されなくなりましたが、 "$ error = 'あなたが検索したもの。戻ってください。 ';'表示されているものです。 –

0

どのようになるかの条件を渡すdoesntの場合。クエリ実行前のトラップ また、できる場合は、クエリビルダ(アクティブレコード)を使用してください。

public function search($search) 
{ 
    $this->db->select('*'); 
    $this->db->from('users'); 
    $this->db->like('username',$search); 
    $this->db->or_like('fname',$search); 
    $this->db->or_like('lname',$search); 
    $this->db->or_like('mname',$search); 
    $query = $this->db->get(); 

    $memb = ''; 
    if ($query->result_array() > 0) { 
     foreach ($query->result_array() as $row) { 
     $memb = $row['idnumber'];   
     } 
     if ($memb) { 

     $this->db->select("*"); 
     $this->db->where("idnumber", $memb); 
     $query1 = $this->db->get("users"); 

     //$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'"); 

     $hehe = $query1->result_array(); 
     return ($hehe) ? $memb : false; 
     } 
     return false; 
    } 

    $error = 'There is no record for the one you searched. Please go Back.'; 
    return $error; 
} 
+0

動作していません:(遅く返事を申し訳ありません) –

+0

何が問題なのですか? –

関連する問題