creating a threaded message system--sorting messages
| From: | Ben Liu | Date: | Thu, 29 Jun 2006 14:11:22 +0000 |
| Subject: | creating a threaded message system--sorting messages | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-238913@lists.php.net to get a copy of this message | ||
This question might deviate from PHP into the domain of MySQL but I
thought best to post here first. I'm building a message board system
with PHP/MySQL. I'm trying to present the messages to the users in
threaded order rather than flat. I'm having a lot of trouble figuring
out how to sort the posts so they appear in the correct threaded
order. I don't think I can do this purely with a SQL query. If it can
be done this way, please suggest how and I'll take this question to
the MySQL list.
I think I have figured out the basic logic, I just have no idea how to
translate it into code. Also, I may have the logic wrong. Anyhow this
is what I have so far:
relevant data structure loosely:
post_id (unique, autoincrement, primary index)
parent_id (if the post is a child, this field contains the post_id of
its parent)
...
1) Query the database for all messages under a certain topic, sort by
parent_id then post_id
2) Somehow resort the data so that each group of children is directly
after their parent. Do this in order of ascending parent_id.
Can this be done with usort() and some programatic logic/algorithm?
How do you sort groups of items together rather than comparing each
array element to the next array element (ie: sorting one item at a
time)? Should this be done with a recursive algorithm?
Anyone with experience writing code for this type of message board, or
implementing existing code? Thanks for any help in advance.
- Ben