0

(sorry for my bad English)

I'm creating a website where users can post something and other users can leave a comment on that post.

I learned from a video from HowCode (youtube) that if you want to display the comments on multiple pages, it is usefull to create 1 page that selects the comments and on all the other pages I can 'include' those comments.

The problem is that it now only displays one comment while I have multiple comments on some posts.

Does anyone know how to solve this?


On the page where I select the comments:

class Comment {
          public static function displayComments($postId) {

                  $comments = DB::query('SELECT * FROM table WHERE post_id = $postId');

                  foreach($comments as $comment) {
                  $commentDisplay = $comment['comment'].' ~ by a user';

                  return $commentDisplay;
                  }
          }
}

On the page where I display the comments:

$showComments = Comment::displayComments($_GET['postid']);
echo $showComments;

3 Answers3

2

Yes, this is happening because you are returning on the first iteration of the foreach.

When you return it immediately exists the method displayComments() and therefore only displays the first one.

What you want to do is return all the comments by adding them to an array and turning the array:

class Comment {
    public static function displayComments($postId) {
        $comments = DB::query('SELECT * FROM table WHERE post_id = $postId');
        foreach($comments as $comment) {
            $commentDisplay[] = $comment['comment'].' ~ by a user';
        }
        return commentDisplay;
    }
}

Then when you call it:

$showComments = Comment::displayComments($_GET['postid']); // this is an array
echo array_values($showComments);

Or use foreach on $showComments if you want to print it in a different way

Oli Girling
  • 605
  • 8
  • 14
0

Instead of returning a single comment, you should push all comments related to the post in an array and return it.

class Comment {
    public static function displayComments($postId) {
        $comments = DB::query('SELECT * FROM table WHERE post_id = $postId');
        $commentDisplay = array();
        foreach($comments as $comment) {
            $commentDisplay[] = $comment['comment'].' ~ by a user';
        }
        return $commentDisplay;
    }
}

And then loop through the returned array to display all comments.

$showComments = Comment::displayComments($_GET['postid']);
foreach($showComments as $comment){
    // display comment 
    // echo $comment . '<br />';
}

Sidenote: Learn about prepared statement because right now your query is susceptible to SQL injection attack. Also see how you can prevent SQL injection in PHP.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Thanks, it works now but I want to display the comments inside an `echo`. I tried inside the foreach `$commentDisplay = $comment;` and then (outside the foreach) `echo 'blah'.$commentDisplay.'blahblah';` but that gave me one comment. Do I have to make the echo inside the foreach so I can use `$comment` to display each comment, or can set a variable inside the foreach? –  Sep 09 '19 at 13:23
  • @HansDeVis, `$commentDisplay` is an array now, you cannot `echo` it to display comments. As stated above, you have to loop through the array to display all comments. Paste your current code snippet(s) in [pastebin.com](https://pastebin.com/) and give me it's link here. – Rajdeep Paul Sep 09 '19 at 18:37
0

You can do it with dependency injection...you should as below more flex more elastic,also data mapping support to user class



class Comment {
         public  function displayComments($postId) {

             $data = DB::query('SELECT * FROM table WHERE post_id = $postId')->fetch(PDO::FETCH_ASSOC)

              return  $data;



          }


}



class User
{

    private $comment;

     private $name;

     private $id;
   function __construct(Comment $comment,int $postid)
   {

       $this->comment=$comment;

         $this->id=$postid;
    }

  function comment()
  {
        return $this->comment->displayComments($this->id)['comment'];

   function name()
   {

    $this->comment->displayComments($this->id)['name'];

     }


 }


$comment=new Comment ();

$user=new User($comment,$postid);

echo $user->comment() .':'.$user->name();
dılo sürücü
  • 3,821
  • 1
  • 26
  • 28