title: why the php function load in wrong place
body: <p>hello I have a problem with my PHP code I have a function to get a comment for each post on my website the function in working but the data is in a wrong place
  as you can see in the picture the comments are in the wrong place and I need to move them to the place in the picture</p>
  <pre><code>&lt;?php
  class posts{
  static function getposts(){
      $query = database::query(&quot;select * from posts join frinds on frinds.user1_id = posts.user_id or 
   frinds.user2_id= posts.user_id  where frinds.user1_id = ? or frinds.user2_id = 
    ?&quot;,array($_COOKIE['uid'],$_COOKIE['uid']));
      if($query-&gt;rowCount() &gt; 0){
          $rows=$query-&gt;fetchAll(PDO::FETCH_ASSOC);
          foreach($rows as $row){
               $id = $row['user_id'];
              // if($id != $_COOKIE['uid']){
                  if($id != $_COOKIE['uid']){
                      $post_text = $row['text'];
                      $id= $row['id'];
                      echo posts::postbody($id,$post_text);
                 
                    
                  }
                  
                  
          }
          if($id == $_COOKIE['uid']){
              $query = database::query(&quot;select * from posts where user_id = ?&quot;,array($_COOKIE['uid']));
              if($query-&gt;rowCount() &gt; 0){
                  $rows=$query-&gt;fetchAll(PDO::FETCH_ASSOC);
                  foreach($rows as $row){
                      $post_text = $row['text'];
                      echo posts::postbody($row['id'],$post_text);
                  }
              }
            }
          }
      }
      static function postbody($id,$post_text){
     
      $body =&quot; &lt;div class='post'&gt;
                      &lt;div class='post_texts' &gt;&lt;p class='post_text'&gt;$post_text&lt;/p&gt;
                      
                      
                      &lt;/div&gt;
                      &lt;div class='comments'&gt;
                     &quot; .posts::comments($id).&quot;
                     &lt;/div&gt;
                      &lt;form method='post'&gt;
                      &lt;input type='text' name='comment'&gt;
                      &lt;input type='submit' name='addcoment'&gt;
                      &lt;/form&gt;
                      
                      &lt;/div&gt; &quot;;
                      return $body;
     }
     static function creatpost(){
         $query = database::query(&quot;INSERT INTO `posts` (`user_id`, `text`) VALUES (?, 
   ?);&quot;,array($_COOKIE['uid'],$_POST['text']));
    }
    static function comments($id){
      $query = database::query(&quot;select * from comments join posts on comments.post_id = posts.id where 
    posts.id = ?&quot;,array($id));
      if($query-&gt;rowCount() &gt; 0){
          $rows=$query-&gt;fetchAll(PDO::FETCH_ASSOC);
          foreach($rows as $row){
              $comment = $row['comment'];
          echo&quot;&lt;p&gt;$comment&lt;/p&gt;&quot;;
          }
          
      }
     }
   }
  ?&gt;
  </code></pre>
  <p>as you can see 
  is where i have the post and &lt;div class'comment'&gt;
  where I should have the comments
  <a href="https://i.stack.imgur.com/sCxiG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sCxiG.png" alt="here is the picture of my error" /></a></p>