Jump to content

how to append comment values to respective posts in php


shan2batman
Go to solution Solved by Psycho,

Recommended Posts

Guys im trying to join a comments table to posts table using the following query in a method.

    public function feedView($session,$friend,$updateid) {
             $sql2=" select  u.update_body,u.author,u.time,u.title,u.account_name,u.update_id,"
                . "c.comment_body, c.os_id,c.author_c,c.time_c,c.comment_id,c.type_c  "
                . "from updates u join comment_update c "
                . "on c.os_id=:statusid WHERE u.account_name = :session or u.account_name=:friend and (u.type = 'a' or 'c') order by u.time asc,c.time_c desc";
        $stmth=  $this->_db->prepare($sql2);
        $stmth->bindValue(":session",$_SESSION['uname']);
        $stmth->bindValue(":friend",$friend);
        $stmth->bindValue(":statusid",$updateid);
        $stmth->execute();
        return $stmth->fetchAll(PDO::FETCH_ASSOC);
        }

it prints the posts fine but the problem is with comments content where it seems to print the comments in the same posts twice. don't know where the bug is coming from.



here is how it is displaying feeds:-

cRfJi.png

Link to comment
Share on other sites

  • Solution

The problem is not the query. The problem is likely the logic in displaying the output. I expect you have a loop to process all the records in the result set. You need to have logic so the first record in the result set display the post content and the first comment (if it exists). Each subsequent record in the result set should only display the comment.

 

But, there is a problem with the query. It uses a normal JOIN. So, if there are no records in the comments table, the post won't be returned either. This needs to use a LEFT JOIN so all relevant posts will be returned - even if they do not have comments.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.