2012-12-01 12 views
0

load.php 파일에서 조건 ($ last_msg_id_db> $ last_msg_id)을 사용하지 않으면 jquery 함수가 올바르게 작동하고 각 0.5 초 후에 데이터를 계속 확인합니다.조건이 작동하지 않는 이유는 무엇입니까?

그러나 조건을 사용하면 jquery 함수는 처음에만 작동하지만 처음 실행 한 후에는 작동하지 않고 작동하지 않습니다.

내 생각에, 나는 올바른 위치에 load.php에 if 조건을 두지 않을 것입니다 .Plz 도움말 또는 다른 대안을 제시하십시오.

PHP 파일 (Load.php)는 JQUERY 기능이있어 여기

$last_msg_id=$_POST['last_msg_id']; 

$sql=mysqli_query($db3->connection,"SELECT * FROM chat_com ORDER by id ASC"); 

$sql_m=mysqli_query($db3->connection,"SELECT max(id) as maxid FROM chat_com"); 
$row_m=mysqli_fetch_array($sql_m); 
$last_msg_id_db=$row_m['maxid']; 
if($last_msg_id_db>$last_msg_id){ 

    while($row=mysqli_fetch_array($sql)){ 

     $textt=$row['mesg']; 
     $textt2="$textt<br>"; 

     $response=array(); 

     $response['msg']=$textt2; 
     $response['last_msg_id_db']=$last_msg_id_db; 
     $final_response[]=$response; 
    } 
} 
echo json_encode($final_response); 

이다.

var last_msg_id = 1; 
function chat_com_one(id, name) { 
    $('#chatcom').show('fast'); 
    (function chatcom_load_one(id, name) { 
    alert(last_msg_id); 
    $.post('load.php', {option:'chatcom_load_one', last_msg_id:last_msg_id},  function(data) { 
     var json = eval('(' + data + ')'); 
     $.each(json, function(i, row) { 
      $("#chatcom #commid #commidwin").append(row['msg']); 
      last_msg_id = row['last_msg_id_db']; 
     }); 
     setTimeout(chatcom_load_one(id, name), 500); 
    }); 
}(id, name)); 
$('#chatcom_send').click(function() { 
    $.post('send.php', { option:'chat_com_send_one', text:$('#chatcom_text').val(), tocom:id}, function(data) { 
     document.getElementById('chatcom_text').value = ''; 
    }); 
}); 
} 
+0

을 가질 수있다 ??? –

+0

@SherinJose 데이터베이스에 새 메시지가 있는지 확인하고 있습니다. 그리고 id (마지막 id = last_msg_id_db가 될 것입니다)가 jquery에 전달되고 그 값이 jaquery var last_msg_id와 같은지 확인하십시오. last_msg_id_db가 jquery last_msg_id보다 크면 메시지 만 가져옵니다. –

+0

3 번 줄 ($ sql = ...)을 아래로 이동하십시오. 바로 전에 while ($ row = mysqli_fetch_array ($ sql)) {. – wormhit

답변

1

$final_response이 두 번째 아약스 요청 후 모든 데이터가 없을 수 있습니다 조건이 $final_response에 값을 설정하는 데 실패 할 수 있기 때문에이 일을하지 않은 수 있습니다.

당신은 if 조건의 상단에 $final_response를 초기화하는 것을 목적으로 당신이 condition..what는 값입니다 .. 확인을 위해

$final_response = array(); 
if($last_msg_id_db>$last_msg_id){ 
    ... 
    ... 
} 
echo json_encode($final_response); 
+0

고마워요. –

+0

어떻게 새로 추가 된 메시지에 다른 색상을 추가 할 수 있습니까? –

+0

Ajax 스크립트에서'$ ("chatcom #commid #commidwin")와 같은 것을 시도해보십시오 append (''행 [ 'msg'] + '' 색상을 설정합니다. –

관련 문제