长轮询php foreach循环数据

时间:2014-05-30 08:34:53

标签: php jquery ajax

我想知道它是否可能或者如何使用长轮询来获取ajax来更新来自php foreach输出数据的数据。到目前为止,我的代码设置如下。

<?php
    if ( $posts ) {

        foreach ( $posts as $post) :

        $postid = $posts['id'];
        $request_posts_comments = regular_query(
        "SELECT a.from_who, 
                a.dateposted, 
                a.topostid, 
                a.commenttext, 
                b.firstn, 
                b.lastn,
                c.defaultphoto
         FROM comments a
         INNER JOIN users b 
         INNER JOIN userprofiles c
         ON a.from_who = b.id
         AND b.id = c.user_id
         WHERE a.topostid = :postid", ["postid" => $post_idr], $conn);
?>
             <div class="divwrap">
                 <div class='posttext'><?php echo $post['posttext']; ?></div>
                 <div class='postcomments'>
                    <?php  
                        foreach ( $request_post_comments as $comments) :
                    ?>
                 <div class="commentdiv"><?php echo $comments['text']; ?></div>
                    <?php endforeach; ?>
                 </div>

             </div>
<?php

        endforeach; }
?>

我想要的是:当有人更新帖子并说我的朋友检查帖子页面并且他正在阅读评论然后我从其他地方发表评论时,我希望评论在没有他重新加载的情况下出现页。因此,如果任何包含评论的帖子我希望它们在没有重新加载网页的情况下淡入,只有当对帖子有新评论时...所以我希望这个问题有意义...

2 个答案:

答案 0 :(得分:0)

在页面加载时,您将使用php显示所有评论。之后,评论部分将每隔n秒使用ajax进行刷新。

(function poll() { // will execute immediately the first time it is called before honouring the wait/timeout interval.
    $.ajax({
        url: "/server/page.php",
        success: function(data) {
            $('.comments').html(data); // replacing the comments section with the data returned from server
        },        
        complete: setTimeout(function() {poll()}, 5000), // calling poll function again
        timeout: 2000
    })
})();


       <div class="comments"> // this is the area where comments will be populated using ajax

        </div>

答案 1 :(得分:0)

$(function() { // paste this to the script tag

                function doPoll() {
                    var arr = [];
                     $('.divwrap').each(function() {
                        arr.push($(this).attr('id').replace(/[a-z_]+/, ''));
                    });
                    console.log(arr);
                    $.post('test2.php',{data:arr.toString()},function(response) {
                        var data = JSON.parse(response);
                        var count = Object.keys(data).length
                        if(count){  // process results here
                            $.each(data,function(id,obj){
                                var id = "#post_"+id;
                                $.each(obj.comments,function(i,cmnt){
                                    $(id).find('.postcomments').append('<div class="commentdiv">'+cmnt+'</div>')
                                })
                            });
                        }
                        setTimeout(doPoll, 5000);
                    });
                }
                doPoll();
            });

//现在在主体上显示通常的PHP脚本,显示页面加载上的帖子

<body>
        <?php
        $posts = array(array('id' => 1, 'posttext' => 'test1', 'comments' => array('text' => 'comment1')),
            array('id' => 2, 'posttext' => 'test2', 'comments' => array('text' => 'comment2')),
            array('id' => 3, 'posttext' => 'test3', 'comments' => array('text' => 'comment3')));
        if ($posts) {
            $str = 'lll';
            foreach ($posts as $post) :

                $postid = $post['id'];
                $postArr[] = $postid;
                $request_post_comments = $post['comments'];

                //$request_posts_comments = regular_query(
                // "SELECT a.from_who, a.dateposted, a.topostid, a.commenttext,b.firstn,b.lastn,c.defaultphoto FROM comments aINNER JOIN users b 
                //INNER JOIN userprofiles c ON a.from_who = b.id AND b.id = c.user_id WHERE a.topostid = :postid", ["postid" => $post_idr], $conn);
                ?>
                <div class="divwrap" id="post_<?php echo $postid ?>">
                    <div class='posttext'><?php echo $post['posttext']; ?></div>
                    <div class='postcomments'>
                        <?php
                        foreach ($request_post_comments as $comment) :
                            ?>
                            <div class="commentdiv"><?php echo $comment; ?></div>
                        <?php endforeach; ?>
                    </div>

                </div>
                <?php
            endforeach;
        }
        ?>

    </body>

//和test2.php你必须根据我们传递的帖子id来定义你的逻辑以获取帖子评论

<?php
 $posts = explode(',',$_POST['data']);
 if(1){ // here check in a loop any new comments for the post ids thth we pass
     $posts = array('1'=>array('comments'=>array("New comment1","New comment2")),'3'=>array('comments'=>array("New comment4","New comment5")));
     echo json_encode($posts);
 }