Answer the question
In order to leave comments, you need to log in
How to organize a stepped commenting model a la "Habr" in PHP + MySql?
The question is, in fact, in the title. What connections in the muscle to use, how to properly organize the output?
Answer the question
In order to leave comments, you need to log in
Recursion with queries to the database, apparently, is not very suitable.
In addition to commentID and parentID, I also used topicID to make a selection based on it.
Further, in php, this is re-formed into a tree and the entire tree is cached in a serialized form.
And the recursion is already in the output, depending on the layout. with frameworks, alas, I can not tell.
Recursion to help you. Select all comments that have parent_comment_id = 0 and for each of them execute the get_child_comments($comment_id) function recursively.
The structure of the database table is something like this: comment_id(int), parent_comment_id(int), comment_text(text) and further as desired.
If you use the Yii framework, then you can implement it using the NestedSet behavior code.google.com/p/yiiext/downloads/detail?name=trees_0.95.zip&can=2&q=
Google about nested sets. On any frameworks and ORMs there are ready-made implementations. For example, in cakephp, doctrine is, and in others like yii there are third-party extensions (or maybe already built in too).
Here are some useful tree links:
the comment ended, here they are :
www.rsdn.ru/article/alg/binstree.xml
doc.prototypes.ru/database/trees/nestedsets/theory/use/base.vingrad.ru/view/1491-Derevo-katalogov-NESTED
-SETS-vlozhennyie-mnozhestva-i-upravlenie-im
www.phpclub.ru/detail/article/db_tree
Recursion in the database (request in a request) is the death of a project. I display the entire tree in one query (ordered by topic_id by time) and build the tree directly in the data processing loop.
It is better to store the entire path (list of parent-id) to the comment, then the tree of comments can be selected with one request (this is how, in my opinion, they do it in LiveJournal).
After all the comments from the database related to the post are received in one request, make up a tree-like php array. In parallel, maintain a linear array, the keys of which are the id of the comment, and the values are links to the elements of the tree. Such a “reference” list is needed in order to simply add nested comments indicating only the parent id and not to search the entire tree where to put a new comment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question