PHP uses recursion to generate an article tree,

Source: Internet
Author: User

PHP uses recursion to generate an article tree,

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 '<ul class="article-list-no-style border-bottom">';      }else{        echo '<ul class="article-list-no-style">';      }          }    if($fatherid==0){      echo '<li><span class="glyphicon glyphicon-log-in"       aria-hidden="true" id="han'.$record['id'].'">      </span>  <a href="'.$record['url'].'" target="_blank">'       . $record['title'].'</a>';    }else{      echo '<li><span class="glyphicon glyphicon-chevron-right" aria-hidden="true">      </span> <a href="'.$record['url'].'" target="_blank">'       . $record['title'].'</a>';    }        category_tree($record['id']);    echo '</li>';    $i++;    if ($i > 0){      echo '</ul>';    }  }}

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.