[PHP] Re: Re: creating a threaded message system--sorting messages

2006-06-29 Thread Ben Liu

On 6/29/06, Adam Zey [EMAIL PROTECTED] wrote:


Just throwing an idea out there, but you can do the sorting entirely in
the SQL query. The trick is to figure out the best way.

The first idea that came to mind (and it sucks, but it works), is a text
field with padded numbers separated by dots, and the number is the
position in relation to the parent. So, with this:

Post 1
  Post 3
   Post 5
   Post 6
  Post 4
   Post 7
Post 2
  Post 8

Now, to the helper field would contain this for each post:

Post 1: 1
Post 2: 2
Post 3: 1.1
Post 4: 1.2
Post 5: 1.1.1
Post 6: 1.1.2
Post 7: 1.2.1
Post 8: 2.1

Now, by pure ascii sorting in that field, that would sort out to:

Post 1: 1
Post 3: 1.1
Post 5: 1.1.1
Post 6: 1.1.2
Post 4: 1.2
Post 7: 1.2.1
Post 2: 2
Post 8: 2.1

Which is the correct sort order. The depth of the post (how far to
indent it?) could be told in PHP by counting the number of periods, or
storing it in the database.


The indentation part I've already sort of figured out--it uses some
php to create a divs-within-divs structure, then using some simple CSS
I can get the appropriate level of indentation.


Now, how to figure out what to put in that field for each post? We need
to do two things. First, each post needs to store the number of
children. Next, when a new post is made, we do three things (Keeping in
mind that in real life I'd pad each entry in the sort helper field
with zeros on the left up to some large number):

1) Get the sort helper field of the parent and the parent's child count
field
2) Take the parent's sort help field, and add on a period and the
parent's child count plus one, insert the child.
3) Update the parent's child count.

OK, now, this method sucks. It's slow, and limits the number of child
posts to whatever you pad (In itself, not a big issue, if each post can
only have, say, a thousand direct childs (which each themselves can have
a thousand childs), not a huge issue). The slow part from ascii sorting
everything is the problem, I'd think.

I've never done threaded anything before, so I assume there's a better
solution. I'm just saying that the job CAN be done entirely with SQL
sorting. And probably faster than your proposed method of resorting
everything once PHP gets ahold of it. It should be noted that you'd need
each post to have a sort of superparent field that stored the topmost
parent so that you could do something simple like selecting ten
superparents and all their children.



Regards, Adam.



Thanks for the help Adam, I'll try to wrap my head around your response...

- Ben

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



RE: [PHP] Re: Re: creating a threaded message system--sorting messages

2006-06-29 Thread KermodeBear
Sounds like that darn Hierarchial Data again. Give this a read and see if it
helps you out at all. In particular, you may find the area 'Finding the
Depth of the Nodes' helpful.

http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

Hope it helps,
-K.Bear

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



[PHP] Re: Re: creating a threaded message system--sorting messages

2006-06-29 Thread Ben Liu

SOLVED, almost. I read the article suggested by K.Bear and found the
recommended solution to be a bit more complicated than I wanted to
implement. I then found another way to do this using the existing
Adjacency List Model through a recursive function. So basically, you
query the database for the post_id of the very first post in the
discussion. You then call a function that displays that post, searches
for all children of that post and then calls itself recursively on
each of the children it discovers. This works great for spitting out
the posts in the proper order. Now I'm trying to figure out how to
make sure the indenting looks right in the browser.

- Ben

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