首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP: Longpolling - $current_row & $latest_row

PHP: Longpolling - $current_row & $latest_row
EN

Stack Overflow用户
提问于 2013-05-12 00:17:29
回答 1查看 76关注 0票数 0

在我的JavaScript长轮询脚本中,我调用msgsrv.php来回显屏幕上还没有出现的num_rows。下面是正在运行的JavaScript:

代码语言:javascript
复制
<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
    /* Simple helper to add a div.
    type is the name of a CSS class (old/new/error).
    msg is the contents of the div */
    $("#notiWrap.notiZero").html(
        "<div id='notiZero "+ type +"'>"+ msg +"</div>"
    );
}

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            addmsg("notiMsg", data); /* Add response to a .msg div (with the "new" class)*/
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */

            );
        },
    });
};
$(document).ready(function(){
    waitForMsg(); /* Start the inital request */

});
</script>

但是,msgsrv.php知道数据库中实际的最新行,但它现在不知道加载到页面上的最新行的id。$num_rows工作;$loaded_rows失败。$loaded_rows会失败,因为当JS调用msgsrv.php时,它会重置为数据库的实际num_rows。我需要PHP来读取页面上的$loaded_rows,而不是数据库中的,因为我已经知道如何查询最新的行。

这是我的msgsrv.php:

代码语言:javascript
复制
<?php
/* Send a string after a random number of seconds (2-10) */
sleep(2);

include('/home/alimix/public_html/include/conn.php');

/* Variables */
$nr = mysql_query("SELECT * FROM txtr");
while(mysql_fetch_array($nr))
{$nowrecent =  mysql_num_rows($nr);}

$mr = mysql_query("SELECT * FROM txtr ");
for( $i=0; $i < 1; $i++ )
{$mostrecent = "$nowrecent";}
/* $mostrecent shouldn't equal $nowrecent if new rows aren't loaded*/
    $minus = $nowrecent - $mostrecent;

        if ($minus > 0) {
            //echo $nowrecent;
            echo $minus;
            //echo $mostrecent;
        }
        elseif($minus < 1) {
            echo "nr".$nowrecent."";
            echo " ";
            echo "mr".$mostrecent."";
        }


?>
EN

回答 1

Stack Overflow用户

发布于 2013-05-12 02:59:12

而不是提供这个:

代码语言:javascript
复制
    url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

输入以下内容:

代码语言:javascript
复制
    url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",
    data: {
      lastID: mylastID
    },

每次获得一行时,将mylastID设置为新的最新ID (并确保mylastID在AJAX调用的范围内)。就是这样,你可以使用$_GET‘’lastID‘来恢复这个值!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16499232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档