PHP中的文章树怎么利用递归生成

发布时间:2020-12-10 15:12:42 作者:Leah
来源:亿速云 阅读:108

本篇文章给大家分享的是有关PHP中的文章树怎么利用递归生成,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

数据库好设计,无非用id,fatherid来进行归类,fatherid代表父类是那篇文章的id,id是文章的唯一id,层次不限,可以是两层,可以是三层。fatherid为0的表示顶层文章。

php代码,主要是递归

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>&nbsp;&nbsp;<a href="'.$record['url'].'" target="_blank">' 
      . $record['title'].'</a>';
    }else{
      echo '<li><span class="glyphicon glyphicon-chevron-right" aria-hidden="true">
      </span>&nbsp;<a href="'.$record['url'].'" target="_blank">' 
      . $record['title'].'</a>';
    }
    
    category_tree($record['id']);
    echo '</li>';
    $i++;
    if ($i > 0){
      echo '</ul>';
    }
  }
}

调用:

category_tree(0) //先提取最顶层文章

以上就是PHP中的文章树怎么利用递归生成,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. PHP 生成 Trie 树
  2. 怎么在javascript中利用递归绘制一个结构树

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 递归

上一篇:如何在php项目中实现一个面包屑导航功能

下一篇:怎么在PHP项目中对函数进行加密与解密

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》