AJAX股利刷新从数据库中通过jQuery.load [英] AJAX Div Refresh from Database via jQuery.load

查看:109
本文介绍了AJAX股利刷新从数据库中通过jQuery.load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个插件的Word preSS产生的内容的jQuery.accordion列表,它从数据库中拉出。我也有它通过jQuery.load方法,通过一个单独的页面(update.php)拉动新的结果从数据库中每5秒。

我现在遇到的问题是,我生成了完整的HTML在update.php文件的分区。好像我只能让它查询数据库,然后将结果返回给主文件,但我不知道如何去这样做。基本上,我关心这里是效率,因为的方式,我做的事情可能是相当繁重的浏览器的资源。我有一种预感,内存使用量将继续攀升使用这种方法,直到它不可避免地高峰和浏览器崩溃。

我在寻找如何更好地code本的任何指导。我提供了概念证明的例子下面,所以我很乐意为任何人提供的反馈,无论是积极还是至关重要的。谢谢!

main.php:

 <脚本类型=文/ JavaScript的>
$(函数(){
  $(#手风琴)负载(update.php);
  变种刷新=的setInterval(函数(){
    $ K(#手风琴)的负载(update.php)。
  },5000);
});
< / SCRIPT>

< D​​IV ID =手风琴>< / DIV>
 

update.php:

 < PHP
功能page_update(){
  全球WPDB $;
  $ OUT ='';

  $ OUT ='<脚本类型=文/ JavaScript的SRC =jQuery的-1.5.min.js>< / SCRIPT>';
  $ OUT ='<脚本类型=文/ JavaScript的SRC =的jQuery UI的1.8.11.custom.min.js>< / SCRIPT>';
  $ OUT ='<脚本类型=文/ JavaScript的>
           变量$ J = jQuery.noConflict();
           $ J(下函数(){
             $Ĵ(#手风琴)手风琴()。
           });
           < / SCRIPT>';

  $ SQL =SELECT * FROM表顺序名DESC;;
  $结果= $ wpdb-> get_results($ SQL);

  的foreach($结果$水库){
    $ OUT ='< H3>'$水库>的名字。'< / H3>';
    $ OUT ='< D​​IV ID =内容 - $水库>的名字。'>'$水库>得分'< / DIV>';。
  }

  回声$输出;
}

page_update();
?>
 

解决方案
  1. 您不应使用global关键字。 <一href="http://stackoverflow.com/questions/5341131/php-performance-and-memory-issue-with-global-variables">Why?

  2. 在你重新绘制所有的手风琴的div,你应该是检查是否存在数据库中的任何变化的函数。如果没有变化,其不需要重新绘制

  3. 如果有变化,你可以只重绘受影响的行。

I have written a plugin for Wordpress which generates a jQuery.accordion list of content that it pulls from a database. I also have it pulling newer results from the database every 5 seconds via a separate page (update.php) through the jQuery.load method.

The problem I'm encountering is that I'm generating the full HTML for the div in the update.php file. It seems like I could just make it query the database and then return the results to the main file, but I'm not sure how to go about doing that. Basically, my concern here is efficiency, because with the way I'm doing things it may be considerably taxing on browser resources. I have a hunch that memory usage will continue to climb with this method until it inevitably peaks and the browser crashes.

I'm looking for any guidance on how to better code this. I've provided proof-of-concept examples below, so I'd love for anyone to offer feedback, whether constructive or critical. Thank you!

main.php:

<script type="text/javascript">
$(function() {
  $("#accordion").load("update.php");
  var refresh = setInterval(function() {
    $k("#accordion").load("update.php");
  }, 5000);
});
</script>

<div id="accordion"></div>

update.php:

<?php
function page_update() {
  global $wpdb;
  $out = '';

  $out .= '<script type="text/javascript" src="jquery-1.5.min.js"></script>';
  $out .= '<script type="text/javascript" src="jquery-ui-1.8.11.custom.min.js"></script>';
  $out .= '<script type="text/javascript">
           var $j = jQuery.noConflict();
           $j(function() {
             $j("#accordion").accordion();
           });
           </script>';

  $sql = "SELECT * FROM table ORDER BY name DESC;";
  $results = $wpdb->get_results($sql);

  foreach($results as $res) {
    $out .= '<h3>'.$res->name.'</h3>';
    $out .= '<div id="content-'.$res->name.'">'.$res->score.'</div>';
  }

  echo $out;
}

page_update();
?>

解决方案

  1. You should not use the global keyword. Why?

  2. Before you redraw all your accordion div, you should make a function that check if there is any changes in the database. If there is no changes, their is no need to redraw.

  3. If there is a change, you could redraw only the affected row.

这篇关于AJAX股利刷新从数据库中通过jQuery.load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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