我想通過JSON/jQuery列出最近活躍的用戶。但是,這種計數系統並不像應該那樣工作。它列出了用戶並給出了「沒有活動用戶」的消息。我怎麼能解決這個問題?Jquery - 計數JSON元素
謝謝!
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));
?>
jQuery的,抓住所述JSON元件
<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>
爲什麼大家都投票反對這個問題?它在我的最終格式正確 - 但現在接受@ dakshbhatt21的更正 – sixli