To avoid this, you need to get the comments out of the database in the order in which they are to be displayed. Take the following example
1 Comment
3 ¦- reply to Comment 1
8 -¦-- reply to comment of a comment
7 ¦- another reply to comment 1
2 Comment
5 ¦- reply to comment 2
6 ¦- another reply to comment 2
4 Comment
The numbers down the left are the autoincremented ID numbers, and are the order in which the comments are posted. The varibles I see that make sense to be stored in the database are ID - id of comment, ParentID ( i.e. comment 5's parent is 2 ) , RootID - the id of the comment's root ( i.e. comment 8's root is 1 )
Now, can I somehow, in one sql query, using that data, order the records as above? Or alternatly, if someone else has a way to do this with a slightly different table layout that would be cool too.
Is it not preferable to select and expand only user-chosen threads? Start with the latest messages that are immediate children of the forum, and allow the user to dictate what gets opened further.
So your query could be to select a certain number of messages, sorted by date, that have the particular forum as their parent.
I apologise if that sounds somewhat "you don't want that, and you don't start here". My only excuse is time spent as an Oracle consultant :)
Would you really want to expand all threads initially? That presupposes that all threads are equally important to the viewer and, by doing that, limits the amount of new information that is displayed per 'screen'.
Well, the first 3 levels will display the title and the content of the comment, and the deeper levels will just display the title, but it doesn't really affect the query.
Basicly like how slashdot etc works
does exactly what I want, but uses recursive mysql queries! which means on a thread of 100 there'll be 100 queries. not exactly scalable!
[edited by: jatar_k at 3:59 pm (utc) on Mar. 12, 2003]
[edit reason] fixed sidescroll link [/edit]
Threads:
ThreadID ( auto-increment )
Comments:
CommentID ( auto-increment )
ThreadID
ReplyTo
DatePosted
SELECT * FROM comments
WHERE ThreadID = '".$threadid."'
ORDER BY ReplyTo, DatePosted
The first comment in a thread has its ReplyTo value set to itself. As the CommentID is auto-incremented you can guarentee any reply will have a higher number than the previous comment.
By sorting by the date/time posted you get the correct order. The just use a CSS rule to indent them all.
asp