PHP infinite level classification, find the ID of the top-level classification through the subclass ID

From , 3 Years ago, written in PHP, viewed 239 times.
URL https://pastebin.vip/view/d5d7f5bb
  1. // 自己基于ThinkPHP写的,亲测可用
  2. /**
  3.  * [getNavPid 假设顶级分类的父ID是0,根据传入的子分类ID,查找子分类的顶级父类ID]
  4.  * @param  [int]  $id   [传入的子类ID]
  5.  * @return [int]        [返回顶级分类ID]
  6.  */
  7. function getNavPid($id){
  8.         $nav = M('nav')->find($id);
  9.         if($nav['f_id'] != 0){ return getNavPid($nav['f_id']); }
  10.         return $nav['id'];
  11. }
  12.  
  13. // 以下两段是网友分享的,没有测试过
  14. function foo($id) {
  15.         $rs = mysql_query("select * from tbl_name where id='$id'");
  16.         $r = mysql_fetch_assoc($rs);
  17.         if($r['pid'] != 0) return foo($r['pid']);
  18.         return $r['id'];
  19. }
  20.  
  21. function tree($arr,$id){
  22.         static $list=array();
  23.         foreach($arr as $u){
  24.                 if($u[‘pid’]== $id){
  25.                         $list[]=$u;
  26.                         If($u[‘pid’]>0){
  27.                                 tree($arr.$u[‘pid’]);
  28.                         }
  29.                 }
  30.   }
  31.   return $list;
  32. }
  33.  
  34.  
  35.  

Reply to "PHP infinite level classification, find the ID of the top-level classification through the subclass ID"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website