我正在尝试通过 JSON/jQuery 列出最近活跃的用户。然而,这个计数系统并没有按应有的方式工作。它既列出了用户,又给出了“没有活跃用户”的消息。我该如何解决这个问题?
谢谢!
获取活跃用户的 PHP 代码:
$liveView_single = array();
$liveView_array = array();
while ($row = mysqli_fetch_assoc($result)){
extract($row);
$liveView_single['id'] = $id;
$liveView_single['type'] = $type;
$liveView_single['username'] = $username;
$liveView_single['lastip'] = $lastip;
$liveView_single['lastactivitypage'] = $lastactivitypage;
$liveView_full[] = $liveView_single;
}
echo(json_encode($liveView_full));
?>
抓取 JSON 元素的 jQuery
<script type="text/javascript">
//counter for number of JSON elements
var i=0;
//show active users function
function showActiveUsers(){
$('#content').html('');
$('#noActiveUsers').html('');
$.get("array.php", function (activeUsersList) {
$.each(activeUsersList, function (a, b) {
$("#content").append("<tr class=\"userRow\" style=\"display:none\">" + "<td class=\"userId\">" + b.id + "</td>"
"<td class=\"type\">" + b.type + "</td>"
"<td class=\"username\">" + b.username + "</td>"
"<td class=\"lastip\">" + b.lastip + "</td>"
"<td class=\"lastActivityPage\">" + b.lastactivitypage + "</td>" + "</tr>");
$(".userRow").show();
//add to counter of JSON elements
i++;
});
}, "json");
if (i == 0){
$('#heading').hide();
$('#noActiveUsers').append('<h2>There are no active users.</h2>');
$('#noActiveUsers').show('slow');
}
//reset timer
setTimeout("showActiveUsers()", 3000);
}
$(document).ready(function() {
showActiveUsers();
});
</script>