Page 1 of 1

Determining which comment belongs to each article?

Posted: Thu Oct 12, 2006 10:42 pm
by cturner
Can someone please tell me how I can determine which comment belongs to each article and which comment belongs to each username? Showing some code would be appreciated. Thanks in advance.

Posted: Thu Oct 12, 2006 10:51 pm
by hawleyjr
I'm sorry; I have no idea what you are tying to do. Can you please elaborate?

Posted: Thu Oct 12, 2006 11:02 pm
by cturner
The comments, usernames and articles are in a database but the comments and usernames are in one table and the articles are in another. I am wanting the users to add comments to the articles and to display the comments that belong to that article. When a user clicks on a link it should go to another page where the comments will be displayed. The articles will be added via an admin centre.

Posted: Fri Oct 13, 2006 2:57 am
by Rovas
It' very easy it' s about database design put a column (foreign key) in the comments table which referes to the article number (which will be the primary key of the articles table). After that you make a simple SQL statment in which you select the comments from joining of the article and comments table on egality of the two columns that I sugested earlier and displaying them on the page you want.
Read more about designing a database and don' t get scared about the many principles and rules it' s hard at the begining but it will be easy once you get the hang of it.

Posted: Fri Oct 13, 2006 3:13 am
by Oren
Show us the structure of the related tables so we can help you.

Posted: Fri Oct 13, 2006 4:51 am
by ibbo
Two tables.

Articles(id, title, summary, content, published, expires, type)
Article_comments(id, articleID, name, comment).

Add a comment

Code: Select all

insert into Article_comments (articleID, name, comment) (332, "ibbo", "my comment");
Grab article and comments

Code: Select all

select * from Articles a left join Article_comments c on c.articleID=a.id where a.id = 332;
Ibbo

Posted: Fri Oct 13, 2006 6:35 pm
by cturner
Ibbo can you please explain to me what c and a represents which is in the left join code?