私の 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 はデータベース内の実際の最新の行を認識していますが、ページにロードされた最新の行の ID は認識していません。$num_rows は機能します。$loaded_rows は失敗します。JS が msgsrv.php を呼び出すと、データベースの実際の num_rows にリセットされるため、$loaded_rows は失敗します。最新の行を照会する方法を既に知っているため、データベースではなくページの $loaded_rows を読み取るために PHP が必要です。
ここに私の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."";
}
?>