【发布时间】: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