获取新的MySQL条目并动态附加到现有div

时间:2012-06-27 15:21:03

标签: php jquery mysql html ajax

我有一个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:DR;如何在不刷新页面的情况下自动获取新注释并将其附加到上述脚本中生成的注释中。

1 个答案:

答案 0 :(得分:0)

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

这是我提出的一些代码,这是一个通用的想法,但它的工作原理。它将每15秒检查一次新评论。