That requires a recursive function, which can generate heaps of queries.
If you want to list 3 'trees' of 6 levels each, it will take 3 * 6 = 18 queries.
if you list 20 trees of 10 levels; 200 queries. ouch.
One way to cheat would be to simply read all the rows into an array inmemory, and do the recursive functions there, which is much faster.
Another 'cheat' is to add another column to the table, called 'threadid' or 'grandparent'
This field wil hold the id of the 'top-level' row for each child.
That way you can limit the number of rows to fetch to just those that have the correct grandparent, and recursively process those.