我正在尝试在CI中创建实时搜索功能(我从未做过)。我是Web开发的新手,仍然在学习东西。我找到了有关如何操作的小教程http://www.blog.orionwebtech.net/codeigniter-jquery-ajax-live-search/?codekitCB=415037771.748888

我在为我的应用翻译该教程中的代码时遇到问题。我有一个称为属性的表,我希望将输入值与子弹列和名称列进行比较。然后,我希望它在输入值与这些列中的任何一个匹配的实时搜索结果中返回段名和名称。它不会同时匹配,因为该段仅包含数字,而名称包含字母。

这是我想出的代码来尝试执行此操作。

风景:

<div class="something">

    <input name="search_data" id="search_data" class="" value="" data-label="Search for a property, a unit, or an resident..." type="text" />

    <div id="suggestions">

        <div id="suggestionslist">

        </div>

    </div>

</div>


JavaScript:

<script type="text/javascript">
        function ajaxSearch() {
            var input_data = $('#search_data').val();
            if (input_data.length === 0) {
                $('#suggestions').hide();
            } else {

                var post_data = {
                    'search_data': input_data,
                    '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
                };

                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>search/autocomplete",
                    data: post_data,
                    success: function(data) {
                        // return success
                        if (data.length > 0) {
                            $('#suggestions').show();
                            $('#autoSuggestionsList').addClass('auto_list');
                            $('#autoSuggestionsList').html(data);
                        }
                    }
                });

            }
        }
</script>


控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function autocomplete()
    {
        $search_data = $this->input->post('search_data');
        $query = $this->Search_model->get_autocomplete($search_data);

        foreach ($query->result() as $row):
            echo "<li><a href='" . base_url('association/'.$row->slug) . "'>" . $row->name . "</a></li>";
        endforeach;
    }
}
/* End of file search.php */
/* File location: application/controllers */


该模型:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function get_autocomplete($search_data)
    {
        $this->db->select('slug, name');
        $this->db->like('slug', $search_data);
        $this->db->like('name', $search_data);
        return $this->db->get('properties', 10);
    }
}

?>


当我测试它时,我没有得到任何结果。我的测试数据有效,因为数据库中有匹配的行。我究竟做错了什么?

最佳答案

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_autocomplete($search_data)
    {
        $this->db->select('slug, name');
        $this->db->like('slug', $search_data);
        $this->db->like('name', $search_data);
        $query = $this->db->get('properties', 10);
        return $query->result();
    }
}

?>

关于javascript - 使用AJAX和CodeIgniter创建实时搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22020567/

10-11 13:33