使用来自MYSQL的数据自动完成 [英] Autocomplete using data from MYSQL

查看:101
本文介绍了使用来自MYSQL的数据自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在从mysql数据库中搜索数据时使用jquery ui来实现自动完成功能....因为nw正在使用来自数组的数据,但我不知道.如何从数据库中检查它?这是我的代码.

I want to make autocomplete feature using jquery ui when searching data from mysql database....for nw am using data from array, but I dont know. How can I check it from the database? Here is my code.

<script>
$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];

    $( "#tags" ).autocomplete({
        source: result_array 
    });
});
</script> 

有什么帮助吗?

编辑为:

?php  
$q=mysql_query("select rfname from research_details") or die(mysql_error()); 

$array_data = mysql_fetch_array($q);

foreach($array_data as $data) {
 $data1[] = "'".$data."',";
}
$data1 = implode(",",$data1); 
$data = substr($data1,0,-1);


?>
$(function() {

var availableTags = [
<?php echo $data; ?>
];
$( "#tags" ).autocomplete({
source:availableTags 
});
});
</script>

推荐答案

您可以使用以下代码进行编码:

You can code it like the following:

$("#tags").autocomplete({
    source: function(request, response) {
        jQuery.ajax({
            url: '/autocomplete_query.php',
            dataType: 'json',
            success: function(data) {
                response(data);
            }
    });
});

在autocomplete_query.php的SELECT查询中,结果的结构应类似于此数组:

The structure of the result in your SELECT query at autocomplete_query.php should be something similar to this array:

$result = array(0 => "ActionScript", 1 => "AppleScript", 2 => "Asp",...);

然后应返回如下:

echo json_encode($result);

  • 并确保这是autocomplete_query.php上唯一的echo语句.
    • And make sure that that's the only echo statement on autocomplete_query.php.
    • 这篇关于使用来自MYSQL的数据自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆