2013-07-13 6 views
-1

JSON/jQuery를 통해 최근에 활성 사용자를 나열하려고합니다. 그러나이 계산 시스템은 제대로 작동하지 않습니다. 그것은 모두 사용자를 나열하고 "활성 사용자 없음"메시지를 제공합니다. 어떻게 해결할 수 있을까요?JSON 요소를 Jquery로 계산합니다.

감사합니다. 활성 사용자를 잡고

PHP 코드 :

<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> 
+0

왜 모두가이 질문에 반대 투표합니까? 내 말미에 올바르게 형식화되었지만 이제는 @ dakshbhatt21의 수정 사항을 받아 들였습니다 – sixli

답변

1

Ajax 요청은 기본적으로 비동기 적으로 실행되는 JSON 요소 (따라서 이름 아약스)을 잡고

$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를. 스크립트는 서버에 ajax 요청을하고 immedeatelly 스크립트 (if(i==0))를 계속 사용합니다. 데이터가 준비되면 성공 기능 (function (activeUsersList))으로 계속 진행합니다. 이 문제를 해결하기 위해 ajax 요청을 동기식으로 만들 수 있습니다 (이 경우에는 이유가 없습니다, the docs 참조). 다른 방법은 "no users online message"를 콜백 함수로 옮기는 것입니다. 이것은 온라인 사용자가 있는지 제대로 판단하기 위해 ajax 요청의 데이터가 필요하기 때문에 더 논리적입니다.

다음 예제에서는 i을 제거하고 if (i == 0) 블록을 콜백 함수로 옮겼습니다.

$.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 
    }); 

    //Move this into the callback function. 
    if(activeUsersList.length == 0) { 
     $('#heading').hide(); 
     $('#noActiveUsers').append('<h2>There are no active users.</h2>'); 
     $('#noActiveUsers').show('slow'); 
    } 


}, "json"); 
+0

감사합니다.하지만 표의 제목 만 유지합니다. IF 문 내에서 아무 것도하지 않습니다. JSON 요소에 대한 카운터를 추가하려면 어떻게해야합니까? 단순히 카운트의 변화를 모니터링해야하기 때문에 ... 감사합니다! – sixli

+0

'activeUsersList.length'에는 json 목록의 항목 수가 포함되어 있습니다. 이것은 귀하의 코드를 읽으면 온라인 사람들의 양과 같습니다. – Sumurai8

+0

위와 똑같은 코드를 사용했지만 활성 사용자가 없을 때 IF 블록이 실행되지 않습니다 ... 이유가 무엇입니까? EDIT PHP가 오타가 있었는데 ... 고마워요! 숫자가 증가했는지 감소했는지 비교하기 위해 'activeUsersList.lenght'를 어떻게 사용할 수 있습니까? – sixli