PHP使用递归生成文章树

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP使用递归生成文章树脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

因为自己的一个站,以文章为主,文章有些是一个系列的,所以想把这些文章归类,同一类的在一个下面。

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

PHP代码,主要是递归

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

      }
      }

      脚本宝典总结

      以上是脚本宝典为你收集整理的PHP使用递归生成文章树全部内容,希望文章能够帮你解决PHP使用递归生成文章树所遇到的问题。

      如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

      本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
      如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。