PHP uses recursion to generate article trees
When writing recursive functions, you can consider caching and defining some static variables to store the results of the previous run. Multi-program operation efficiency is very helpful. Probably The steps are as follows: first get the data from the database, put it into an array, then convert the data into a tree-shaped array, and finally convert the tree-shaped array into html code. Let’s take a look at an example
Because I have a technical website that mainly focuses on articles, and some of the articles are in a series, so I want to classify these articles and put them under the same category.
The database is well designed. It is nothing more than using id and fatherid to classify. fatherid represents the parent category and is the id of the article. id is the unique id of the article. There is no limit to the level. It can be two or three levels. A fatherid of 0 indicates a top-level article.
PHP code, mainly recursive
?
2 3
|
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 '
|
1 | category_tree(0) //Extract the top-level article first |
The above is the entire content of this article, I hope you all like it.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
