2016-10-20 5 views
0

データベースからデータを取得したいが、レコードを取得できない。
私はjsonを使用してデータベースからレコードをフェッチしています。ここjsonを使用してCodeigniterのDBからデータを取得する

は図:

<p id="result"></p> 

<script> 
    $("#txt_category_item_name").on("keyup",function(e) 
    { 
     $("#searchSuggestionList").css({"display":"block"}); 
      var input_searchValue=$("#txt_category_item_name").val(); 
      $.ajax({ 
      type: "POST", 
      url: "search_suggestion_list", 
      data: {recive_value: input_searchValue}, 
      dataType: 'json', 
      cache: false, 
      success: function(recive_result) { 
       $("#result").html(recive_result[1]); 
      } 
     }); 
    }); 
</script> 

コントローラ:

<?php 
    class Main_ctrl extends CI_Controller { 
     function search_suggestion_list() { 
      $this->load->model('main_model'); 
      $recive_search_value=$this->input->post('recive_value'); 
      $data=array(); 
      $recive_search_value; 
      $data['recive_search_result']=$this->main_model->search_suggestion_list_model($recive_search_value); 

      $this->load->view('pages/search_suggestion_list_result',$data); 
     } 
    } 
?> 

とモデル:

<?php 
    class Main_model extends CI_Model { 
     function search_suggestion_list_model($recive_search_value) { 
      $this->load->database(); 

      $this->db->select('company_id,company_name,company_address,company_category,company_keywords,company_state,company_city,company_website_address'); 

      $this->db->from('company_information'); 

      $this->db->like('company_category',$recive_search_value); 
      $query=$this->db->get(); 

      return $query->result(); 
     } 
    } 
?> 
+0

URLべき index.php/.... ajaxリクエストで –

+0

'search_suggestion_list'コントローラメソッドを教えてもらえますか?そのコントローラーはどのコントローラーですか? – DFriend

+0

ブラザー私はコントローラーのsearch_suggestion_listメソッドを指摘しました –

答えて

0

ビューでベロー

を試してみて、コントローラ内

<p id="result"></p><script> 
$("#txt_category_item_name").on("keyup",function(e) 
{ 
    $("#searchSuggestionList").css({"display":"block"}); 
     var input_searchValue=$("#txt_category_item_name").val(); 
     $.ajax({ 
     type: "POST", 
     url: "/main_ctrl/search_suggestion_list", 
     data: {recive_value: input_searchValue}, 
     dataType: 'json', 
     cache: false, 
     success: function(recive_result) { 
      if(recive_result.error){ 
       alert(recive_result.error) 
      } 
      else{ 
      $("#result").html(recive_result.success); 
      }  
     } 
    }); 
}); 

<?php 
class Main_ctrl extends CI_Controller { 
    function search_suggestion_list() {  
     if($this->input->post('recive_value')){ 
      $this->load->model('main_model'); 
      $recive_search_value=$this->input->post('recive_value'); 
      $data=array();    
      $data['recive_search_result']=$this->main_model->search_suggestion_list_model($recive_search_value); 
      if($data['recive_search_result']){ 
       $data['error'] = 'No Records found'; 
      } 
      else{ 
       $data['success'] = $data['recive_search_result'][1]['name']; // edit as you wish 
      } 
      echo json_encode($data);exit; 
     }    
    } 
} 

モデルでは:?

<?php 
class Main_model extends CI_Model { 
    function search_suggestion_list_model($recive_search_value) { 
     $this->load->database(); 

     $this->db->select('company_id,company_name,company_address,company_category,company_keywords,company_state,company_city,company_website_address'); 

     $this->db->from('company_information'); 

     $this->db->like('company_category',$recive_search_value); 
     $query=$this->db->get(); 

     return $query->result_array(); 
    } 
} 
+0

私は、 'echo json_encode($ data);'の後の 'exit;'文を削除する必要があると思います。 Codeigniterは、コントローラの実行が終了するまで、実際にブラウザに出力を送信しません。 'exit'を使うと通常の出力ロジックが壊れ、何もブラウザに送られません。 – DFriend

+0

私はDBにレコードをいくつか持っていますがレコードが見つからないことを示しています –

+0

"レコードが見つかりません"とjs alert meassageを受け取りますか?そのような場合は、SQLクエリを一度確認してください。 – Satya

関連する問題