简体   繁体   中英

News database with comments, how to get rid of duplicates - with mysql and PHP?

I want to create a news update system, using MySQL and PHP. But i can't get it working, so i can display a news update and all the comments attached (and then a valid user is logged in, the person can delete each comment individually).

I have these four tables and DB schema

news_tbl (news_id, date, user (fk to users_tbl.username), headline, bodytext and picture)
users_tbl (username, email, password, usertype)
comments_tbl (comments_id, name, comment)
news_comments_tbl (news_comments_id, news_id_fk, comments_id_dk)

When a user writes a news update, news_comments_tbl its set with the news_id and comments_id is NULL, then a user writes a comments, news_comments_tbl sets comments_id with an id.

But when there is more then one comment, the news update is duplicated, so it like:

News 1
-No comment

News 2
-1 comment
Comment:
Toms comment

News 3
-2 comment
Comments:
Johns comment

News 3
-2 comment
Comments:
Allans comment

And i want:

News 1
-0 comment

News 2
-1 comment
Comments:
Toms comment

News 3
-2 comment
Comments:
Johns comment
Allans comment

How do i display every news update and all the comments attached to it?
I have tried GROUP_CONCAT - but then i cannot fetch the ID of each comments, so a user can delete the comments individually?
Iv'e also tried with GROUP BY news_tbl.news_id , but then i only get the first comment?
And how do i count the comments? Iv'e tried with this:

COUNT(comments_tbl.comments_id) AS count

But then i only get the first written news?! I'v spent a lot of time trying to get it working, but can't seem to figure it out :( Hope someone can help me

This is my query:

$sqlquery ="SELECT news_tbl.*, news_comments_tbl.*, users_tbl.*, comments_tbl.* 
    FROM news_tbl
    LEFT JOIN news_comments_tbl ON news_comments_tbl.news_id_fk = news_tbl.news_id
    LEFT JOIN comments_tbl ON news_comments_tbl.comments_id_fk = comments_tbl.comments_id
    LEFT JOIN users_tbl ON users_tbl.username = news_tbl.user
    ORDER BY news_tbl.news_id DESC LIMIT 5";

This is my PHP code:

        $result = $conn->query($sqlquery);


        foreach($result as $row)
        {
            $commentsidfk = $row['comments_id_fk'];
            $newsidfk = $row['news_id_fk'];

          if($row['comments_id_fk'] == NULL)
          {
              echo $row['headline'];
              echo '<br>';
              echo $row['bodytext'];
              echo '<br>';
          }
          if($row['comments_id_fk'] != NULL)
          {
              echo $row['headline'];
              echo '<br>';
              echo $row['bodytext'];

              echo '<br>';
              echo $row['comment'];
              echo '<br>';
              echo '<a href=delete.php?news_id='.$commentsidfk.' >Delete comment</a>';
              echo '<br>';

          }
              echo '<br>';
              echo '<a href=delete.php?news_id='.$newsidfk.' >Delete news</a>';
              echo '<br>';
        }

Because you are joining the news to the comments, each row will contain the news and one of the comments.

You may find it more efficient to select the news in one query and grab the comments in a second query as you are shipping more data than you need because of the join. You'll need to measure it to see if it is faster.

Alternatively, you could process this in you loop by only writing news once when you detect a new id (your query already has things ordered by the id so you know they will process in order)

    $currentNewsId = 0;


    foreach($result as $row)
    {
        $commentsidfk = $row['comments_id_fk'];
        $newsidfk = $row['news_id_fk'];

        if ($newsidfk != $currentNewsId) {
             // write the news out
             $currentNewsId = $newsidfk;
        }
        // ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM