【问题标题】:jquery autocomplete in codeigniter dont work propertlycodeigniter中的jquery自动完成功能无法正常工作
【发布时间】:2014-12-14 02:45:49
【问题描述】:

当我搜索时,当我写下姓氏或名字时,它给我的结果不好,数据库中的内容找不到它。空味精也不起作用。谢谢
这是我的控制器:

class Search extends CI_Controller{
  function index(){
    $this->load->view('search_view');
  }

  function search_users(){
    $this->load->model('search_model');
    if (isset($_GET['term'])){
      $q = strtolower($_GET['term']);
      $this->search_model->search($q);
    }
  }
}

这是模型:

class Search_model extends CI_Model{
      function search($q){
        $this->db->select('*');
        $this->db->like('CONCAT(user_profiles.firstn, " ", user_profiles.lastn)', $q);
        $query = $this->db->get('user_profiles');
        $query = $this->db->get('user_profiles');
    $row_set = array();
    $temp = array();

    if($query->num_rows > 0){
        foreach ($query->result_array() as $row){
            $temp['id'] = $row['id'];
            $temp['label'] = $row['firstn'].' '.$row['lastn'];
            $row_set[] = $temp; 
        }
          echo json_encode($row_set);
        }
      }
    }

这是我的观点:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<style type="text/css">
#empty-message { float: left; }
input { float: left }
</style>



<input type="text" id="search" />

<script>

$(function(){
    $('#search').autocomplete({
        source: "search/search_users",
        minLength:0,

        response: function(event, ui) {
            if (ui.content.length === 0) {
                $("#empty-message").text("No results found");
            }
            else {
                $("#empty-message").empty();
            }
        }

    });


});
</script>

如果我使用 $this->db->last_query() 并将“a”放在标签中,它会给我这个查询:

SELECT *
FROM (`user_profiles`)
WHERE  CONCAT(user_profiles.meno, " ", user_profiles.priezvisko)  LIKE '%a%'

这是结果:

[{"id":"5","label":"Janosik icloud"},{"id":"7","label":"Janosik iicloud"},{"id":"8","label":"Janosik Smrdi"}]

但是如果我把其他字母写成空白页没有最后一个查询没有结果
这是我的桌子
id -- user_id -- 角色 -- firstn -- lastn --birthd -- 城市头像 -- profile_pic
1 -- 1 -- 行政 - 艾康—— 艾科诺夫—— 1990-06-09 -- 日丽娜—— 无效的 - 空 --

5 -- 6 -- 用户—— 亚诺西克—— icloud—— 1999-05-06 -- 日丽娜—— 无效的 - 空 --

6 -- 7 -- 用户—— 艾康—— 外表 - 1800-06-08 -- 无效的 - 无效的 - 空 --

7 -- 8 -- 用户—— 亚诺西克—— iicloud—— 1000-08-09 -- 无效的 - 无效的 - 空 --

8 -- 9 -- 用户—— 亚诺西克—— 斯米尔迪—— 0000-00-00 -- 无效的 - 无效的 - 空 --

【问题讨论】:

  • 你试过 LOWER(CONCAT(NANES)) 和 strtolower($string) 吗?
  • 我试了一下,谢谢,但它工作只有当我把第二个字母查询时它返回空白页

标签: php jquery codeigniter


【解决方案1】:

我希望这会对你有所帮助 在控制器中

function search($q)
{
    $this->db->select('firstn,lastn');
    $this->db->like('CONCAT(user_profiles.firstn, " ", user_profiles.lastn)', $q);
    $query = $this->db->get('user_profiles');
    $row_set = array();
    $temp = array();
    if($query->num_rows > 0)
    {
        foreach ($query->result_array() as $row)
        {
            // give your id
            $temp['id'] = $row['id']; 
            // give your label
            $temp['label'] = $row['label']; 
            $row_set[] = $temp;
        }
    }
     echo json_encode($row_set);
     die;
}

在视图中

<script>

$(function(){
    $('#search').autocomplete({
        source: "search/search_users",
        minLength:0,
        response: function(event, ui) {
            var c = ui.content.length;
            check_data(c);
        }

    });
});

function check_data(data)
{
    if(data === 0)
    {
          $("#empty-message").text('No results found');
    }
    else
    {
        $("#empty-message").text( '' );
    }
}
</script>

【讨论】:

  • 感谢回复,但它仍然只从 db 中搜索几个名字,例如我有大约 7 个名字,它仍然找到相同的名字(大约 3 个)在其他情况下它什么也没找到并且不显示 msg跨度>
  • 请您提供您在数据库中的测试示例和名称?您可以使用 $this->db->last_query() 调试您的查询;并检查您的查询是否正确?
  • 在 MySQL 服务器上运行调试查询比较两个结果。
【解决方案2】:
  • 在控制器方法的开头打印变量$q
  • 在模型方法前打印变量$q
  • 打印查询运行以检查您的$q 是否正确发送到您的模型。

Doc Codeigniter - last_query()

【讨论】:

  • q 发送正确,当我只放一个字母时,我放第二个什么都没有发生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多