php - 如何在 php 中从 sql 创建自动完成

标签 php html mysql database autocomplete

嗨,首先对不起我的英语,我有这个代码来获取输入中的数据并在用户输入时显示在列表中 但我的数据在 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/

相关文章:

php - 从 MySQL 表中解析空行并输出为没有空单元格的 html 表

mysql - Laravel 5,加入两个单独的查询调用?

php - 当 pecl4win 关闭时从哪里获取 php_perl.dll?

html - 链接 SVG 图像的最佳方式是什么?

HTML CSS 导航菜单定位文本和图像

Javascript 确认对话框

php - 通过SQL获取订单数超过2次但登录次数少于3次的客户

php - Laravel 类邮件程序不存在

php - 将静态导航菜单更改为wordpress动态菜单

mysql - 使用 MySQL 时如何最大化工程/功能灵活性?