During the design of BB, I have been thinking about whether it is possible to display the structure of an infinite classification and search for the parent and child structures without recursion, if the algorithm here is not optimized, the consequences may be fatal! Think about it. If the number of comments in an article is 300, you have to query the database for at least 301 times based on the normal recursive algorithm, and the database is still not nested, if there are one or two levels of nesting or over 1000 comments, isn't the database directly down?
In fact, PHP's powerful array processing capabilities can help us solve this problem quickly and conveniently. The following figure shows an infinitely classified
IDparentID newsID commts
108 comments with Article ID 8
21 8 reply to a comment with ID 1
328 reply to a comment with ID 2
To present the comment of Article 8 in the nested form at the front end, we only query the database once, that is, "SELECT * from table where newsID = 8 ", the post-recursion work is handed over to a powerful PHP array. The possible problem here is the reorganization of the structure relationship of the array. All comments staying on the first-level classification will be placed under their parentID to form the children item.
Next we will paste the code in the BBComment class. I hope to share with you my ideas and propose better and more efficient algorithms.
|The code is as follows:||Copy code|