2013-01-15 3 views
0

Possible Duplicate:
Server Side PHP Long polling서버 측 긴 폴링 PHP

내가 그것을 꿔 일을 내 PHP는 점점 진짜 문제에 봉착했습니다. 서버를 오래 폴링하고 아래의 모든 정보를 출력하려고합니다.

내가보기에 잘못된 것을 볼 수 있습니까? 난 그냥 정확한 세부 정보를 출력해야합니다 .. 서버 측에 및 content은 어떤 알 수없는 이유로 number 4을 출력하고 있습니다. 그리고 클라이언트 측에서 나는 RESPONSE NULL을 얻고 있습니다.

<script> 
setInterval("streamrefresh();", 2000); 

streamrefresh = function(){ 
    var timestamp = "<? echo $streamitem_data['streamitem_timestamp']; ?>"; 

    var last_msg_id = "<? echo $streamitem_data['streamitem_id']; ?>"; 
     $.ajax({ type: "POST", 
       url: "include/streampostinsert.php", 
       data:{ "timestamp":timestamp, "last_msg_id":last_msg_id}, 
       dataType:"json", 
       cache:false, 
       success: function(response){ 
        $("#homestatusid").prepend("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr><div id='status_text_holder'>"+newmsg+"</div><div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'></div><table width=100%><tr><td valign=top width=30px><div class='form'><form id='mycommentform' method='POST' class='form_statusinput'><input type='hidden' name='streamidcontent' id='streamidcontent' value='"+response['streamitem_id']+"'><input type='input' name='commentingcontents' id='commentingcontents' placeholder='Say something' autocomplete='off'><input type='submit' id='button' value='Feed'><br/></div></div>"); 
       } 
     }); 
    } 
</script> 

서버 측 : 이것은 당신이 당신의 비교를 수행 할 때 $last_msg_id은 항상 0 또는 1 중 하나라는 사실과 아마 부분적으로

$lastmodif = isset($_POST['timestamp'])?$_POST['timestamp']:0; 
$last_msg_id = isset($_POST['last_msg_id']) ? $_POST['last_msg_id'] : 0; 


$final_response = array(); 
$check = mysqli_query($mysqli,"SELECT streamitem_content,streamitem_timestamp FROM streamdata WHERE streamitem_id>'$last_msg_id' AND streamitem_status='1' AND streamitem_target=".$_SESSION['id'].""); 
$resultArr = mysqli_fetch_array($check); 
$checks= mysqli_query($mysqli,"SELECT MAX(streamitem_id) AS max FROM streamdata WHERE streamitem_target=".$_SESSION['id'].""); 
$row = mysqli_fetch_array($checks); 
$last_msg_id_db = $row['max']; 

if($last_msg_id_db>$last_msg_id) 
{ 
    $final_response['streamitem_id'] = $last_msg_id_db['streamitem_id']; 
    $final_response['streamitem_content'] = $last_msg_id_db['streamitem_content']; 
    $final_response['streamitem_timestamp'] = Agotime($last_msg_id_db['streamitem_timestamp']); 

    $check1 = mysqli_query($mysqli,"SELECT * FROM users WHERE id=".$_SESSION['id'].""); 
    $resultArr = mysqli_fetch_array($check1); 
    $final_response['username'] = $resultArr['username']; 
    $final_response['id'] = $resultArr['id']; 
    $final_response['first'] = $resultArr['first']; 
    $final_response['middle'] = $resultArr['middle']; 
    $final_response['last'] = $resultArr['last']; 

    echo json_encode($final_response); 

} 
+0

고객 측 코드를 볼 수 있습니까? – halfer

+0

물론. 1 순간 – dave

+0

$ resultArr = mysqli_fetch_array ($ check1); SB mysqli_fetch_row? – Cups

답변

0

.

당신은 여기에 첫 번째 줄을 변경해야합니다 :

$last_msg_id = isset($_POST['last_msg_id']) ? $_POST['last_msg_id'] : 0; 

POST에서 last_msg_id을하거나이 설정되지 않은 경우 0으로 기본 것이다.

+0

그는이 경우 어떻게 '숫자 4'를 얻겠습니까? – Sahal

+0

나는 문제가 json 파싱으로 생각한다. –

+0

@Sahal 포스트의 코드는 첫 번째 게시물 이후로 업데이트되었지만 잘못되었다 (변수를 설정하는'isset()'에 대한 직접적인 호출). –