0
這適用於英文字符,但不適用於阿拉伯語。我打印查詢與 「فف」 爲$title
,我發現了奇怪的字符,它是這樣的:Codeigniter中的JQuery自動完成編碼
SELECT * FROM users WHERE agent_id = 1 AND type LIKE "owner" AND (firstName LIKE "%\%D9\%81\%D9\%81%" OR lastName LIKE "%\%D9\%81\%D9\%81%")
這裏是我的代碼: 控制器:
public function getResultExtend($title) {
$user_id = $this->session->userdata('id');
$result = $this->user->getOwners($user_id, $title);
foreach ($result as $user) {
$user->userName = utf8_encode(stripslashes($user->userName));
}
echo json_encode($result);
}
查看:
$(document).ready(function(){
$('#search').keypress(function(e){
if(e.which == 13)
{
e.preventDefault();
}
var searched = $('#search').val();
var fullurl = $('#hiddenurl').val() + 'index.php/agent/getResultExtend/' + searched;
$.getJSON(fullurl,function(result){
var elements = [];
$.each(result,function(i,val){
elements.push({ value: this.ID, label: this.firstName+' '+this.lastName});
});
$('#search').autocomplete({
source : elements,
select: function (event, ui) {
console.log(ui.item);
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
},
select: function(event, ui) {
$("#search").val(ui.item.label);
$("#owner_id").val(ui.item.value);
return false;
}
});
});
});
});
型號:
function getOwners($user_id, $title) {
header('Content-Type:text/html; charset=UTF-8');
//$title = iconv(mb_detect_encoding($title, mb_detect_order(), true), "UTF-8", $title);
$q = $this->db->query('SET NAMES utf8');
$query = $this->db->query('SELECT * FROM users
WHERE agent_id = ' . $user_id . ' AND type LIKE "owner"
AND (firstName LIKE "%'.$this->db->escape_like_str($title).'%"
OR lastName LIKE "%'.$this->db->escape_like_str($title).'%")
');
return $query->result();
}
我的表排序規則是utf8_unicode_ci
,列也是utf-8。 那麼,問題在哪裏?
在PHPMyAdmin中嘗試查詢後,我發現它工作正常。 – Yasmeen