我现在正在做一个简单的聊天,它已经可以工作了,但效率不是很高。每当我运行脚本时,它都会刷新整个聊天并一遍又一遍地加载内容。
我试图找到一种方法,让脚本只从我的聊天数据库加载新内容,而不应该一遍又一遍地加载旧消息
这是我的index.php中的部分:
<div id="chat" class="full rund">
<table border="1" class="full chattable">
<?php
$abfrage = "SELECT * FROM chat";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)) {?>
<tr class="chattr">
<td style="width:200px;"><h3 class="text">ƬψƬ ☢ <?php echo $row->name;?>:</h3></td>
<td style="width:440px;"><p><h3 class="text"><?php echo $row->text;?></h3></p></td>
<td style="width:160px;"><h3 class="text"><?php echo $row->timestamp;?></h3></td>
</tr>
<?php
}
?>
</table>
</div>
这是我的聊天.js:
setInterval( "updateShouts()", 1000 );
function fx(form,target){
var _target=target;
var url=form.action;
var data=$(form)[(form.method.match(/^post$/i))?'serializeArray':'serialize']();
$(_target).load(url,data,function(){setTimeout(function(){
$(_target).empty();
},0);});
return false;}
function updateShouts(){
$('#chat').load('php/latestMsg.php');
}
MylatestMsg.php(与索引相同):
<div id="chat" class="full rund">
<table border="1" class="full chattable">
<?php
$abfrage = "SELECT * FROM chat";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis)) {?>
<tr class="chattr">
<td style="width:200px;"><h3 class="text">ƬψƬ ☢ <?php echo $row->name;?>:</h3></td>
<td style="width:440px;"><p><h3 class="text"><?php echo $row->text;?></h3></p></td>
<td style="width:160px;"><h3 class="text"><?php echo $row->timestamp;?></h3></td>
</tr>
<?php
}
?>
</table>
</div>
我希望任何人都能帮助我,我已经找了很长时间了,但我找不到任何能解决我问题的东西!感谢:)
2解决方案。
-
当然,你需要一种方法来将你的消息标记为已读,按用户。
-
你可以忽略这一点,只回读最后的X条消息。
-
在你的聊天表中,你必须添加"未读"字段或其他内容,以识别消息状态(已读/未读),就像@geggleto解决方案一样
-
将过滤器添加到您的查询中,如下所示:
$abfrage = "SELECT * FROM chat WHERE timestamp between '$from' AND '$to' ";
以确保您有一个按时间限制消息的角色