首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取新的MySQL条目并动态附加到现有的div

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

Stack Overflow用户
提问于 2012-06-27 23:21:03
回答 1查看 1.1K关注 0票数 0

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

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

代码语言:javascript
复制
<?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;我如何在不刷新页面的情况下自动获取新注释,并将它们附加到上面脚本中生成的注释。

EN

Stack Overflow用户

回答已采纳

发布于 2012-06-27 23:45:22

JQUERY

代码语言:javascript
复制
$(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端

代码语言:javascript
复制
        $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秒检查一次新的评论。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11229645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档