When writing recursive functions, you can consider caching and defining static variables to store the last running result. It is very helpful to run multiple programs. the procedure is as follows: First fetch data from the database, put it in an array, and then convert the data into
When writing recursive functions, you can consider caching and defining static variables to store the last running result. It is very helpful to run multiple programs. the procedure is as follows: First fetch data from the database, put it in an array, and then convert the data into
Because one of my own technical sites focuses on articles and some articles are in a series, I want to classify these articles into one class.
The database is well designed and can be categorized by id and fatherid. fatherid indicates that the parent class is the id of the article, and id is the unique id of the article. The layers are not limited and can be two layers, it can be three layers. If the fatherid is 0, it indicates the top-level article.
Php code, mainly Recursion
Function category_tree ($ fatherid) {// require_once ("mysql_class/config. inc. php "); // require_once (" mysql_class/Database. class. php "); $ db = new Database (DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); $ db-> connect (); $ SQL =" SELECT id, title, url FROM ". TABLE_TASK. "WHERE fatherid = $ fatherid and ispublic = 1 order by id asc"; $ articles = $ db-> query ($ SQL); $ db-> close (); while ($ record = $ db-> fetch_array ($ articles) {$ I = 0; if ($ I = 0) {if ($ fatherid = 0) {echo'
';} Else {echo'
';}} If ($ fatherid = 0) {echo'
- '. $ Record ['title']. '';} else {echo'
- '. $ Record ['title']. '';} category_tree ($ record ['id']); echo'
'; $ I ++; if ($ I> 0) {echo'
';}}}
Call:
Category_tree (0) // extract the top-level article first
The above is all the content of this article. I hope you will like it.