嗨,首先对不起我的英语,我有这个代码来获取输入中的数据并在用户输入时显示在列表中 但我的数据在 phpmyadmin 的 sql 数据库中,我在 php 中是新的 我想在 php 中从 sql 创建自动完成功能,但我不知道谷歌中有大量示例,但没有一个适用于 utf-8 字符集 我的数据库中有 utf-8 单词,所以我不能这样做
这对我有用,但没有 sql
(function( $ ) {
var proto = $.ui.autocomplete.prototype,
initSource = proto._initSource;
function filter( array, term ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
return $.grep( array, function(value) {
return matcher.test( $( "<div>" ).html( value.label || value.value ||
value ).text() );
});
}
$.extend( proto, {
_initSource: function() {
if ( this.options.html && $.isArray(this.options.source) ) {
this.source = function( request, response ) {
response( filter( this.options.source, request.term ) );
};
} else {
initSource.call( this );
}
},
_renderItem: function( ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( $( "<a></a>" )[ this.options.html ? "html" : "text" ](
item.label ) )
.appendTo( ul );
}
});
})( jQuery );
$(function() {
var availableTags = [
{ label: 'سیب', value:'سیب' }, //<===utf-8
{ label: '2سیب', value:'سیب2' },//<===utf-8
{ label: 'Apple3', value:'Apple3' }
];
$( "#tags" ).autocomplete({
source: availableTags,
html: 'html'
});
});
我在这里得到了代码表: http://jsfiddle.net/eay3d/1/
最佳答案
您可以使用 PHP 填充 JQuery 数组。如果不知道有关该表的更多信息,我无法为您提供完整/工作代码,但它看起来像这样:
var availableTags = [
<?php
$queryString = "SELECT * FROM `table`"; // I don't know what your table is called
$query = mysqli_query($conn, $queryString) or die('Unable to execute query. ' . mysqli_error($conn));
while ($row = mysqli_fetch_array($query)){
echo "{ label: '" . $row['dataLable'] . "', value:'" . $row['dataValue'] . "' },"; // I don't know what your fields are called
}
?>
];
此示例还需要正确设置数据库连接。您可以在 W3Schools 上阅读相关内容:Open a Connection to MySQL
关于php - 如何在 php 中从 sql 创建自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46472992/