Retrive Data from DB in Codeigniter using json

时间:2016-10-20 13:05:45

标签: javascript php json ajax codeigniter

I want to retrieve data from a database but I can't get any record.
I am using json to fetch record from the database.

Here is the view :

<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>

The controller :

<?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);
        }
    }
?>

And the model :

<?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();
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

尝试Bellow

在视图中

<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();
    }
}