簡體   English   中英

使用json從Codeigniter中的數據庫檢索數據

[英]Retrive Data from DB in Codeigniter using json

我想從數據庫中檢索數據,但無法獲取任何記錄。
我正在使用json從數據庫中獲取記錄。

這是視圖:

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

試試貝婁

視野中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM