I am facing a decree about handling threaded comments in our project ... I have a simple All the comments which have the MySQL table There are two types: Parents and children children represent one answer to one parent or another child.
My problem:
- Comment (depth 0)
- Answer Children (Depth 1)
Answer to previous child Give (depth 2)
-Tiping (Depth 0)
Imagine the structure above and a MySQL query with my Linux 2. This is the last answer (depth 2) Actually I say something like this Would you like to try: Limit up to 2, if the child does not go to the next parent. Tried many questions with no luck ...
What I have just followed is as follows:
Select from SQL_CALC_FOUND_ROWS * Comments where comment_post_id = '{$ _REQUEST ["id "]} 'Comment_id by ORDER, comment_date DESC serious 10"
Important table fields are:
comment_id (index) | comment_parent_id (note of guardian or null is _ID) | comment_date
I'm very grateful
MySQL does not have any ideas!
< P> Work to parse structures like a tree. In the simplest scenario (the child's parent has an ID), you can revisit the program in an introductory manner to find all the sub-nodes in a given node. Maxwellwell wants to point you to the depth it decreases with each recurring call so that you end up with 0, which will stop the recurrence.For example (Pseudo-code)
findNodes (string par EntId, int maxLevel) (select from * positions, where parent = parent underwork (results ...) {if (maxLevel & gt; 0) {findNodes (result.nodeId, maxLevel-1)} doSomethingWIthAResult}} To do this in a more concise way, there are several techniques, all of which are indexed in some way The path in which the path of the current post is included, the path may look something like this: TopNode: Child1: Child2: Child3 ... in which you can do such a selection * Choose from the posts where the path like "TopNode%" and Depth = 2.