drvntaomy06331839 2013-06-20 12:40
浏览 53
已采纳

使用MYSQL中的数据自动完成

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> 

any help?

Edit to:

?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>
  • 写回答

6条回答 默认 最新

  • duan1982453 2013-06-20 13:04
    关注

    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);
                }
        });
    });
    

    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",...);
    

    And then should be returned like this:

    echo json_encode($result);
    
    • And make sure that that's the only echo statement on autocomplete_query.php.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型