0

我有一个 PHP 脚本来选择数据库中的评论记录,然后将它们打印到页面上。我想要发生的是,如果一个用户在一个页面上,而另一个用户对所述页面上的一个项目发表评论,它会自动将新评论附加到底部。我遇到的问题是我不知道如何区分所有状态。

我用于生成状态评论的代码是:

<?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:博士;如何在不刷新页面的情况下自动获取新评论并将它们附加到上述脚本中生成的评论中。

4

1 回答 1

0

查询

$(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 端

        $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';
        }

这是我想出的一些代码,它是一个通用的想法,但它有效。它将每 15 秒检查一次是否有新评论。

于 2012-06-27T15:45:22.060 回答