长轮询PHP foreach循环数据


long polling php foreach loop data

我想知道是否可能或者如何让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; }
?>

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

$(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脚本,显示页面加载时的post

<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你必须定义你的逻辑来获取基于我们传递的post id的post评论

<?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);
 }

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

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