Determining which comment belongs to each article?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Determining which comment belongs to each article?

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I'm sorry; I have no idea what you are tying to do. Can you please elaborate?
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post 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.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Show us the structure of the related tables so we can help you.
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post 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
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Ibbo can you please explain to me what c and a represents which is in the left join code?
Post Reply