PHP: Longpolling - $current_row & $latest_row

В моем скрипте длинных опросов JavaScript я вызываю msgsrv.php num_rows еще не на экране. Вот 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 знает фактическую последнюю строку в базе данных, но теперь не идентифицирует последнюю строку, загруженную на страницу. $num_rows работает; Сбой $loaded_rows. Сбой $loaded_rows, потому что когда JS вызывает msgsrv.php, он сбрасывается в фактические значения базы данных num_rows. Мне нужен PHP, чтобы прочитать, что такое $loaded_rows на странице, а не в базе данных, потому что я уже знаю, как запросить последнюю строку.

Вот мой msgsrv.php:

<?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."";
        }


?>

1 ответ

Вместо кормления это:

    url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

Поток это:

    url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",
    data: {
      lastID: mylastID
    },

Каждый раз, когда вы получаете строку, установите mylastID на ваш новый последний идентификатор (и убедитесь, что mylastID входит в объем вызова AJAX). Вот и все, затем вы можете восстановить значение, используя $_GET['lastID']!

Другие вопросы по тегам