tags:

views:

107

answers:

2

So I have these tables:

Topics
id, title, date

Posts
id, title, text, date, user, topic_id

How should I structure it so that the first post, the topics text, is on top of all other posts in the topic? Sort them by date? Or is there a smarter way?

+3  A: 

Yes, sort by date (or by post ID). How else would you do it with that database structure?

ceejayoz
what if i add pagination?
It would not matter if you added pagination, because with pagination you should be using LIMIT.
Chris B.
You'd use MySQL's LIMIT clause for that.
ceejayoz
that's what i just said lol
Chris B.
Yes, that happens when two people type at once.
ceejayoz
A: 

Query

mysql_query("SELECT * FROM posts SORT BY date ASC");

Actually now that I look at this you may want to add time and sort by time and date, so that way two posts in the same day are shown in the correct order. It is not very clean to sort by id.

When adding pagination, you will be using MySQL's LIMIT to choose the records for that page. So if you are showing 20 records per page the query would look as so.

1st page:

mysql_query("SELECT * FROM posts SORT BY date ASC LIMIT 0,20");

2nd page:

mysql_query("SELECT * FROM posts SORT BY date ASC LIMIT 20,40");

The first page is calling records 0 through 20. The second page is calling records 20 through 40.

It will sort them by date and time (maybe) accordingly. I am interested in how your forum will turn out! Let us know!

Best of Luck!! Let me know if you have any questions or concerns.

Chris B.
Chris B, why is LIMIT 0,20 and LIMIT 20,40 out of quotes?
Delirium tremens
Typo. Thanks for pointing that out. By the way delirium tremens is some good stuff. if your speaking of the beer of course.
Chris B.