doutuobao9736 2012-09-11 21:02
浏览 40
已采纳

用php列表加速jquery自动完成

Hi I have run into an issue. I have implemented jquerys famous autocomplete and I am creating a list (quite long) from the database to output into the autocomplete feild. But it is taking too long to find the correct value in list. Does anyone know a way I can speed this up??? Here is my jquery:

<script>
    $(function() {function log( message ) {$( "#destId" ).val( message );}
        $( "#destinations" ).autocomplete({
            source: "destinos.php",
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "" + ui.item.id :
                    "" + this.value );}});});
    </script>

And here is destinos.php:

//connect to database
include 'php/dbconn.php';
$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

$qstring = "SELECT Destination as value, DestinationId as id FROM DestinationId WHERE Destination LIKE '%".$term."%'";
//query the database for entries containing the term
    $result = mysql_query($qstring);
//loop through the retrieved values   
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    {
            $row['value']=htmlentities(stripslashes($row['value']));
            $row['id']=htmlentities(stripslashes($row['id']));
            $row_set[] = $row;//build an array
    }
    echo json_encode($row_set);//format the array into json data

Any help would be greatly greatly appreciated!

  • 写回答

2条回答 默认 最新

  • doujiao7325 2012-09-11 21:09
    关注

    I would start off looking at the DB aspects.

    First, you need to make sure you have an index on Destination.

    Second, you ought to consider using a LIMIT, say 10 or 20 rows. In an autocomplete, in most cases you don't need that many results to display at one time. The match count will decrease as the user continues typing fairly quickly.

    Third, you should use proper mysql escape on $term variable before querying with it.

    The rest looks pretty straightforward.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思