Re: creating a threaded message system--sorting messages

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thursday 29 June 2006 09:11, Ben Liu wrote:

> 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

I've written such a system before more than once.  You do the sorting in SQL, 
and then traverse the data recursively in PHP to build the tree.  It's a 
single SQL query.  Check the archives for this list for about 3-4 weeks ago, 
I think.  We were just discussing it, and I showed some basic code 
examples. :-)

-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux