在我的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失败。$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."";
}
?>发布于 2013-05-12 02:59:12
而不是提供这个:
url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",输入以下内容:
url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",
data: {
lastID: mylastID
},每次获得一行时,将mylastID设置为新的最新ID (并确保mylastID在AJAX调用的范围内)。就是这样,你可以使用$_GET‘’lastID‘来恢复这个值!
https://stackoverflow.com/questions/16499232
复制相似问题