jquery - long polling php foreach loop data -


i wondering if possible or how can ajax update data php foreach outputted data using long polling. far code set following.

<?php     if ( $posts ) {          foreach ( $posts $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          comments          inner join users b           inner join userprofiles c          on a.from_who = b.id          , b.id = c.user_id          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 $comments) :                     ?>                  <div class="commentdiv"><?php echo $comments['text']; ?></div>                     <?php endforeach; ?>                  </div>               </div> <?php          endforeach; } ?> 

what want is: when updates post , friend checks out posts page , he's reading comments post comment somewhere else, want comments appear without him reloading page. if posts containing comments want them fade in without reloading webpage when there new comments post ... hope question makes sense ...

$(function() { // paste 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();             }); 

// on body usual php script shows post on page load

<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 $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 comments ainner join users b                  //inner join userprofiles c on a.from_who = b.id , b.id = c.user_id 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 $comment) :                             ?>                             <div class="commentdiv"><?php echo $comment; ?></div>                         <?php endforeach; ?>                     </div>                  </div>                 <?php             endforeach;         }         ?>      </body> 

// , test2.php have define logic fetch post comments based on post ids pass

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

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -