jQuery UI 自动完成功能到 MySQL 与本地存储?

发布于 2024-12-01 19:42:40 字数 651 浏览 0 评论 0原文

有兴趣使用 jQuery UI 自动完成功能连接大型 MySQL 事件数据库。这会显着减慢每个页面的速度吗?(搜索栏将出现在所有页面上)

我是否应该将查询结果存储到本地存储并将脚本放在页面底部?或者SESSION或者Cookie?

这是没有任何缓存功能的代码。

<?php

require_once("../../connect.php");
$day_events = "SELECT * FROM tbl_events";
$events_result = mysql_query($day_events);

?>

<script>
    $(function() {
    var availableTags = [
<?php

  while($event_row=mysql_fetch_array($events_result)) {
  echo "\"".$event_row['event']."\",\n";
  }

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

**编辑:澄清我的问题是使用自动完成功能会显着减慢页面速度吗?如果可以的话可以用什么方法来改善呢?

Interested in using the jQuery UI autocomplete feature to connect with a large MySQL database of events. Will this slow down each page significantly?(Search bar will be on all pages)

Should I store the results from the Query to Local Storage put the script at the bottom of the page? Or SESSION or Cookie?

Here is the code without any cache features.

<?php

require_once("../../connect.php");
$day_events = "SELECT * FROM tbl_events";
$events_result = mysql_query($day_events);

?>

<script>
    $(function() {
    var availableTags = [
<?php

  while($event_row=mysql_fetch_array($events_result)) {
  echo "\"".$event_row['event']."\",\n";
  }

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

**EDIT: To clarify my question is will using the autocomplete slow down pages significantly? If so what methods can be used to improve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈情不如逗狗 2024-12-08 19:42:40
<script> 
$().ready(function() {
$('#tag').autocomplete('tag.php?find=tag', {
          width: 260,
          matchContains: true,
          selectFirst: false
        });
});
</script>
<?php
//in tag.php
$find = $_GET["find"];
if($find=='tag'){
    $q = strtolower($_GET["q"]);
    if (!$q) return;
    $sql = "select DISTINCT tag from tag where name_tag LIKE '%$q%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_array($rsd)) {
        $cname = $rs['name_tag'];
        echo "$cname\n";
    }
}
?>
<input id='tag' type='text'>
<script> 
$().ready(function() {
$('#tag').autocomplete('tag.php?find=tag', {
          width: 260,
          matchContains: true,
          selectFirst: false
        });
});
</script>
<?php
//in tag.php
$find = $_GET["find"];
if($find=='tag'){
    $q = strtolower($_GET["q"]);
    if (!$q) return;
    $sql = "select DISTINCT tag from tag where name_tag LIKE '%$q%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_array($rsd)) {
        $cname = $rs['name_tag'];
        echo "$cname\n";
    }
}
?>
<input id='tag' type='text'>
双马尾 2024-12-08 19:42:40

您最好使用 AJAX 来检索包含用户键入的字符串的事件,这将过滤您的结果集,并且意味着您的页面也不会花费很长时间来加载。

Your better off using AJAX to retrieve events containing the string the user types, that will filter down your result set and means your page won't take ages to load either.

手长情犹 2024-12-08 19:42:40

根据我的经验,本地和远程自动完成都相当快。我根据将要加载到源中的数据量以及访问数据的概率来使用其中之一。如果您的自动完成功能将搜索大型数据集,则最好使用远程选项(http://jqueryui.com/demos/autocomplete/#remote)。这可能会增加轻微的延迟,具体取决于 mySQL/Web 服务器返回结果的时间以及通过连接发送的数据量。

另一种选择是将源数据保存在外部 JavaScript 文件中,以便浏览器只需要下载一次(它应该自动缓存它)。同样,这取决于有多少数据。如果有相当多的数据,那么使用远程/AJAX 路线可能更有意义。

尽管缓存结果是节省数据库命中的好主意,但不需要将结果放入会话或 cookie 中。自动完成组件有一个缓存选项(http://jqueryui.com/demos/autocomplete/#remote-with-cache),但还有其他方法可以缓存 ajax 请求和 javascript,以便为所有访问者缓存。

In my experience, both local and remote autocomplete has been reasonably fast. I use one or the other depending on how much data is going to be loaded into the source, and what the probability is that the data will be accessed. If there is a large dataset your autocomplete will be searching against, it may be best to use the remote option (http://jqueryui.com/demos/autocomplete/#remote). That may add a slight delay depending on how long the results are returned by mySQL/web server and how much data is sent over the connection.

Another option is to keep the source data in an external javascript file so that the browser only needs to download it once (it should automatically cache it). Again, this depends on how much data there is. If there's quite a lot of data, it may make more sense to go with the remote/AJAX route.

Placing the results in a session or cookie shouldn't be needed, although caching the results is a good idea to save on database hits. The autocomplete component has a caching option (http://jqueryui.com/demos/autocomplete/#remote-with-cache), but there are other ways to cache ajax requests and javascript so that it's cached for all visitors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文