gpt4 book ai didi

php - Codeigniter 中的 JQuery 自动完成编码

转载 作者:行者123 更新时间:2023-11-30 00:40:35 25 4
gpt4 key购买 nike

这适用于英语字符,但不适用于阿拉伯语。我用“فف”打印了 $title 的查询,我发现其中有奇怪的字符,如下所示:

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%") 

这是我的代码: Controller :

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。那么问题出在哪里呢?

最佳答案

检查 application/config/database.php 中的行:

$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';

还尝试添加到您的 .htaccess

AddDefaultCharset UTF-8

或者直接向index.php添加

header('Content-type: text/html; charset=utf-8' );

关于php - Codeigniter 中的 JQuery 自动完成编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com