问题描述
我有一个PHP脚本来选择数据库中的评论记录,然后将它们打印到页面上。我想要发生的事情是,如果用户在一个页面上,并且另一个用户对该页面上的项目发表评论,它会自动将新评论附加到底部。我遇到的问题是我不知道如何区分所有状态。
我在代码中生成注释的代码是:
<?php
$ rt =(SELECT * FROM(SELECT comment as comment,byuid as作为时间戳的UNIX_TIMESTAMP(timestamp)FROM mingle_comments WHERE onuid ='$ sid'AND type ='status'ORDER BY timestamp DESC LIMIT 2)mingle_comments ORDER BY timestamp ASC); //查询
$ result = mysql_query($ rt)或者死(mysql_error());
if(mysql_num_rows($ result)> = 2){
?>
< div id =sastyle =background:#E0E0E0; padding:5px 5px 5px 5px;>
< a href =#style =font-family:Arial; font-size:12px; color:#3a3a3a; text-decoration:none;>查看所有评论...< /一个>
< / div>
}
while($ st = mysql_fetch_assoc($ result)){
$ comment = nl2br($ st ['comment']) ;
$ by = $ st ['byuid'];
$ comuid = $ st ['comuid'];
$ time = $ st ['timestamp'];
$ l = $ st ['likes'];
$ d = $ st ['dislikes'];
$ bq =SELECT * FROM users WHERE uid ='$ by'LIMIT 1;
$ bqq = mysql_query($ bq)或死(mysql_error());
while($ bqr = mysql_fetch_assoc($ bqq)){
$ dp = $ bqr ['dp'];
$ fbq = $ bqr ['fname'];
$ sbq = $ bqr ['sname'];
}
?>
< div id =commentboxclass =<?php echo $ comuid;?> style =padding:5px 5px 5px 5px;>
< div id =cbistyle =display:inline; position:relative;>
< img src =<?php if($ dp == null){echo'img / unknown_user.jpg';} else {echopf /。$ by。/。$ dp ;}?> width =36pxstyle =display:inline; position:relative;/>
< / div>
< div id =cbtstyle =position:relative; margin-left:32px; margin-top:-35px;>
< a href =profile.php?uid =<?php echo $ uid;?> style =position:relative; font-size:13px; margin-left:10px; font-family:Arial; color:#3a3a3a; text-decoration:none;><?php echo $ fbq。 。 $ SBQ; ?>< / A>
< p class =<?php echo $ comuid;?> style =position:relative; margin-left:5px;><?php echo $ comment; ?>< / p为H.
< / div>
< div id =clbstyle =background:#E0E0E0; padding-left:5px;>
< a href =#>与< / a>< a href =#id =time><?php echo time_since($ time); ?>< / A>
< / div>
< / div>
<?php
}
?>
TL:DR;我如何自动获取新评论,并将它们附加到上面脚本中生成的评论中,而不刷新页面。 JQUERY
$(document).ready(function(){
var lastcheck ='';
setInterval(function(){
$ .ajax({
url:'URL_TO_PHP_SCRIPT',
类型:'POST',
数据:{'lastcheck':lastcheck},
成功:函数(结果){$ b $如果(result!='nothing_new'){
lastcheck = result.lastcheck / *更新lastcheck * /
var data = result.data;
$ .each(data,function(i,val){
// foreach评论你做你需要的东西,比如append, prepend等数据[i]。key来获取数值。
});
}
}
});
},15000 / *这会每15秒检查一次,你可以改变它* /);
});
PHP SIDE
$ lastcheck = $ _ POST ['lastcheck'];
/ *
您应该为每个新创建的评论添加一个日期字段,然后按orderby date> $ lastcheck进行过滤,以便在您上次检查后获得评论
您在这里获得新评论
...
...
..
* /
if(!empty($ arrayofnewcomments)){
/ *输出* /
echo json_encode(array('lastcheck'=> date('Ymd H:i:s'),'data'=> $ arrayofnewcomments));
} else {/ *没有新评论* /
echo'nothing_new';
$ / code $
这是我想出的一些代码,这是一个普通的想法,但它的工作原理。它会每15秒检查一次新评论。
I have a PHP script to selected records for comments from a database, and then print them onto the page. What I'd like to happen is that if a user is on a page, and another user comments on an item on said page, it automatically appends the new comment to the bottom. The problem I'm having is that I don't know how to differentiate between all of the statuses.
My code for generating the comments on a status is:
<?php
$rt = ("SELECT * FROM (SELECT comment as comment, byuid as byuid, comuid as comuid, likes as likes, dislikes as dislikes, UNIX_TIMESTAMP(timestamp) as timestamp FROM mingle_comments WHERE onuid = '$sid' AND type = 'status' ORDER BY timestamp DESC LIMIT 2) mingle_comments ORDER BY timestamp ASC"); //query
$result = mysql_query($rt) or die (mysql_error());
if(mysql_num_rows($result) >= 2) {
?>
<div id="sa" style="background:#E0E0E0; padding:5px 5px 5px 5px;">
<a href="#" style="font-family:Arial; font-size:12px; color:#3a3a3a; text-decoration:none;">View all comments...</a>
</div>
<?php
}
while($st = mysql_fetch_assoc($result)) {
$comment = nl2br($st['comment']);
$by = $st['byuid'];
$comuid = $st['comuid'];
$time = $st['timestamp'];
$l = $st['likes'];
$d = $st['dislikes'];
$bq = "SELECT * FROM users WHERE uid = '$by' LIMIT 1";
$bqq = mysql_query($bq) or die (mysql_error());
while($bqr = mysql_fetch_assoc($bqq)) {
$dp = $bqr['dp'];
$fbq = $bqr['fname'];
$sbq = $bqr['sname'];
}
?>
<div id="commentbox" class="<?php echo $comuid; ?>" style="padding:5px 5px 5px 5px;">
<div id="cbi" style=" display:inline; position:relative; ">
<img src="<?php if ($dp == null) { echo 'img/unknown_user.jpg'; } else { echo "pf/" . $by . "/" . $dp; } ?>" width="36px" style=" display:inline; position:relative;"/>
</div>
<div id="cbt" style="position:relative; margin-left:32px; margin-top:-35px;">
<a href="profile.php?uid=<?php echo $uid; ?>" style="position:relative; font-size:13px; margin-left:10px; font-family:Arial; color:#3a3a3a; text-decoration:none;"><?php echo $fbq . " " . $sbq; ?></a>
<p class="<?php echo $comuid; ?>" style="position:relative; margin-left:5px;"><?php echo $comment; ?></p>
</div>
<div id="clb" style="background:#E0E0E0; padding-left:5px;">
<a href="#">Like</a><a href="#" id="time"><?php echo time_since($time); ?></a>
</div>
</div>
<?php
}
?>
TL:DR; How can I automatically fetch new comments and append them to the comments generated in the above script without page refresh.
解决方案 JQUERY
$(document).ready(function(){
var lastcheck='';
setInterval(function() {
$.ajax({
url: 'URL_TO_PHP_SCRIPT',
type: 'POST',
data: {'lastcheck':lastcheck},
success: function(result){
if (result!='nothing_new'){
lastcheck=result.lastcheck /*Update lastcheck*/
var data=result.data;
$.each(data, function(i,val){
//Foreach comment you do what you need, like append, prepend, etc. data[i]."key" to get the value.
});
}
}
});
}, 15000 /* This will check each 15 seconds, you can change it */);
});
PHP SIDE
$lastcheck=$_POST['lastcheck'];
/*
You should add a date field to each new created comment,then filter by "orderby date > $lastcheck" so you get comments since your last check
You get the new comments here
...
...
..
*/
if (!empty($arrayofnewcomments)){
/*The output*/
echo json_encode(array('lastcheck'=>date('Y-m-d H:i:s'),'data'=>$arrayofnewcomments));
}else{/*No new comments*/
echo 'nothing_new';
}
This is some code I came up with, it's a general Idea but it works. It will check each 15 seconds for new comments.
这篇关于获取新的MySQL条目并动态追加到现有的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!