A
A
Arthur Koch2011-01-26 19:48:04
PHP
Arthur Koch, 2011-01-26 19:48:04

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

10 answer(s)
G
gaelpa, 2011-01-26
@dudeonthehorse

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.

M
MOst_53, 2011-01-26
@MOst_53

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.

K
korotovsky, 2011-01-26
@korotovsky

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=

U
un1t, 2011-01-26
@un1t

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:

P
Pavlo Ponomarenko, 2011-01-26
@TheShock

google://hierarchical+structures+in+databases+

U
un1t, 2011-01-26
@un1t

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

A
Alexander, 2011-01-27
@akalend

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.

T
tampere, 2011-01-27
@tampere

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).

T
Timur, 2011-01-27
@timursun

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.

N
nekoval, 2011-06-12
@nekoval

forums and comments often use left-right numbering. This greatly increases the cost of adding new nodes:
http://blogs.sitepoint.com/hierarchical-data-database-2/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question