Jump to content

Display News And Comments


shawy14

Recommended Posts

Hello All, I am new here so first off hello to you all :)

 

Right my problem, I am trying to find a way to be able to display news items with comments under them and a form for somebody to write a comment. Like on facebook where a person has a status then comments under it.

 

I have two tables in MySql already, news and newscomments. The way I have linked the tables is by ID, newscomments has a field called News_ID which matches with the ID of a news item.

 

I can get all news items to display however I can't get the comments to display with them.

 

Please could somebody give me a hand with this?

 

Thanks

 

Richard

 

 

Link to comment
Share on other sites

I guess when you view a news entry your url is like site.com/news.php?id=1. Which displays the news article with the id of 1 from your news table. If thats the cause you can use a JOIN which will query both your news and newscomments table at the same time. Example join query.

 

SELECT 
    n.title, n.author, n.article,
    c.username, c.email, c.coment
FROM news n 
LEFT JOIN newscomments c 
    ON (c.News_ID = n.ID)
WHERE n.ID = $news_id

Link to comment
Share on other sites

Note: to setup a table alias, you need to use the AS keyword:

 

SELECT 
     n.title, n.author, n.article,
     c.username, c.email, c.coment
FROM news AS n 
LEFT JOIN newscomments AS c 
     ON (c.News_ID = n.ID)
WHERE n.ID = $news_id

 

 

You could also use the full table name if you find table aliases confusing:

 

SELECT 
     news.title, news.author, news.article,
     newscomments.username, newscomments.email, newscomments.coment
FROM news 
LEFT JOIN newscomments 
     ON (newscomments.News_ID = news.ID)
WHERE news.ID = $news_id

Link to comment
Share on other sites

I guess when you view a news entry your url is like site.com/news.php?id=1. Which displays the news article with the id of 1 from your news table. If thats the cause you can use a JOIN which will query both your news and newscomments table at the same time. Example join query.

 

SELECT 
    n.title, n.author, n.article,
    c.username, c.email, c.coment
FROM news n 
LEFT JOIN newscomments c 
    ON (c.News_ID = n.ID)
WHERE n.ID = $news_id

 

Hi thanks for this, very fast replying. However if I put /news.php?id=1 that in it doesn't display anything at all. How would I be able to get that to happen?

 

Thanks again

Link to comment
Share on other sites

Right got that working, it is displaying each news item with a comment underneath however if a news item has two comments it displays the news item twice. Once with the first comment and then a second time with the second comment :/

Link to comment
Share on other sites

As the query returns both the news and the comments. You will need to first extract the news article and then add all the comments into an array. Then where you want your comments to display you'll then use a foreach loop to output your 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.