2014-12-14 2 views
0

jquery, php, ajax로 채팅 시스템을 만들었습니다. 채팅 창에 html 로그의 마지막 10 줄을 표시하고 싶습니다. 어떻게채팅 시스템 로그 문제

...

echo "<div id='chatbox'>"; 
if(file_exists("log.html") && filesize("log.html") > 0){ 
$handle = fopen("log.html", "r"); 
$contents = fread($handle, filesize("log.html")); 

fclose($handle); 

$lastfifteenlines = array_slice(explode("<br>",file_get_contents($contents)),-10); 

echo $lastfifteenlines; 
} 
echo "</div>"; 

그러나 로그에서 모든 것을 보여주는 것 :

<div class='msgln'><b>Brian</b>: dd<br></div><div class='msgln'><b>Brian</b>: ddf<br></div><div class='msgln'><b>Arne</b>: PIS!<br></div><div class='msgln'><b>Brian</b>: sdfsdf sdfsdffds sdfdsf sdf sdf dfs dfsdf sdf sfd sfd fsd fsd sdf sdffsd sd fsdfsd fsd fsd<br></div><div class='msgln'><i>Brian er kommet på chatten.</i><br></div> 

내 PHP는 이제 다음과 같습니다

log.html이 같이 보입니다 마지막 10 줄만 보여줄 수 있습니까? 마지막 10 번은 br이 발생 했나요?

편집 :

아약스와 JQuery와 :

function loadLog(){  
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request 
$.ajax({ 
url: "log.html", 
cache: false, 
success: function(html){   
$("#chatbox").html(html); //Insert chat log into the #chatbox div 

//Auto-scroll   
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request 
if(newscrollHeight > oldscrollHeight){ 
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div 
}    
}, 
}); 
} 
+0

왜 이런 종류의 데이터베이스를 사용하지 않습니까? 형식화 된 데이터를 구문 분석하는 것은 결코 좋은 생각이 아닙니다. – Hidde

답변

0
function get_last_lines($fp, $num) 
{ 
    $idx = 0; 

    $lines = array(); 
    while(($line = fgets($fp))) 
    { 
     $lines[$idx] = $line; 
     $idx = ($idx + 1) % $num; 
    } 

    $p1 = array_slice($lines, $idx); 
    $p2 = array_slice($lines, 0, $idx); 
    $ordered_lines = array_merge($p1, $p2); 

    return $ordered_lines; 
} 

// Open the file and read the last 15 lines 
$fp = fopen('log.html', 'r'); 
$lines = get_last_lines($fp, 15); 
fclose($fp); 

// Output array 
print_r($lines); 

가 자기하여보십시오.

+0

지금 시도했지만 작동하지 않습니다 ... 채팅 시스템을 처음 사용하는 경우 자습서를 사용 했으므로 실제로 내가 무엇인지 정확하게 알지 못합니다. 하고 ...하지만 배우기. jquery 부분이 PHP 부분을 덮어 쓰고있는 것 같습니다. 그래서 내가하는 일에 상관없이, 여전히 전체 로그를 보여줍니다 ... jquery에서 "마지막 10 행 표시"부분을 만들어야합니다. ? – Zjitzu