why the php function load in wrong place

Viewed 62

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

<?php
class posts{
static function getposts(){
    $query = database::query("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 = 
  ?",array($_COOKIE['uid'],$_COOKIE['uid']));
    if($query->rowCount() > 0){
        $rows=$query->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("select * from posts where user_id = ?",array($_COOKIE['uid']));
            if($query->rowCount() > 0){
                $rows=$query->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 =" <div class='post'>
                    <div class='post_texts' ><p class='post_text'>$post_text</p>
                    
                    
                    </div>
                    <div class='comments'>
                   " .posts::comments($id)."
                   </div>
                    <form method='post'>
                    <input type='text' name='comment'>
                    <input type='submit' name='addcoment'>
                    </form>
                    
                    </div> ";
                    return $body;
   }
   static function creatpost(){
       $query = database::query("INSERT INTO `posts` (`user_id`, `text`) VALUES (?, 
 ?);",array($_COOKIE['uid'],$_POST['text']));
  }
  static function comments($id){
    $query = database::query("select * from comments join posts on comments.post_id = posts.id where 
  posts.id = ?",array($id));
    if($query->rowCount() > 0){
        $rows=$query->fetchAll(PDO::FETCH_ASSOC);
        foreach($rows as $row){
            $comment = $row['comment'];
        echo"<p>$comment</p>";
        }
        
    }
   }
 }
?>

as you can see is where i have the post and <div class'comment'> where I should have the comments here is the picture of my error

1 Answers

The issue is that your static method call posts::comments($id) already uses echo to output the comments. While the post body is stored in the variable $body which is echoed only later on.

You need to change you code such that the comments are not immediately echoed when they are collected from the database. But that they are either placed in an output buffer or returned to the calling scope. So that they can become part of the actual post body you try to create.

In other words: when calling posts::comments($id) you expect to get something back which you try to concatenate to your $body variable. But your method never returns something. It only creates output that is send to the client immediately. So it never gets part of that variable where you try to collect the post body.


UPDATE:

How can you "collect" the comments to return them all together?

static function comments($id){
    $query = database::query("select * from comments join posts on comments.post_id = posts.id where 
  posts.id = ?",array($id));

    if($query->rowCount() > 0) {
        $rows = $query->fetchAll(PDO::FETCH_ASSOC);

        $comment = [];
        foreach ($rows as $row) {
            $comments[] = $row['comment'];
        }
        return implode("\n", $comments);
    }
    return "<p>No comments so far...</p>;
}
Related