首頁 >後端開發 >php教程 >php无限分类读取多级菜单_PHP教程

php无限分类读取多级菜单_PHP教程

WBOY
WBOY原創
2016-07-13 17:48:581627瀏覽

//数据库表

 \

 

include "comm.php"; 
 
//获取分类菜单  
 
$arr=getTree(); 
$arrone=getFlone(59); 
$arrtwo=getFltwo(93); 
$arrmenu=getFlmenu(59); 
print_r($arrmenu); 
 
 
//获取分类树  
function getTree(){ 
    global $db; 
    $sql="select flid,flpid,flpath,fltitle,fldes,keyword from fenlei order by flid"; 
    $rs=$db->query($sql); 
    $data=array(); 
    while ($row=$db->fetch_array($rs)){ 
        $data[]=$row; 
    } 
    return $data; 

//活动一级分类 注意在这顶级分类id为 59  
function getFlone($flpid){ 
    global $db; 
    $sql="select * from fenlei where flpid={$flpid} order by flid"; 
    $rs=$db->query($sql); 
    $data=array(); 
    while ($row=$db->fetch_array($rs)){ 
        $data[]=$row; 
    } 
    return $data; 

//活动二级分类  
function getFltwo($flpid){ 
    global $db; 
    $sql="select * from fenlei where flpid={$flpid} order by flid"; 
    $rs=$db->query($sql); 
    $data=array(); 
    while ($row=$db->fetch_array($rs)){ 
        $data[]=$row; 
    } 
    return $data; 

//循环获取菜单,此数据库顶级父分类id为 59   
function getFlmenu($flpid){ 
    global $db; 
    $arr=getFlone($flpid); 
 
    foreach ($arr as &$v) { 
        $arrt=getFlone($v['flid']); 
         
      foreach ($arrt as &$vl){ 
        $arrtt=getFlone($vl['flid']); 
        $vl['tt']=$arrtt; 
      } 
        $v['two']=$arrt; 
    } 
    return $arr; 

 
?> 
include "comm.php";

//获取分类菜单

$arr=getTree();
$arrone=getFlone(59);
$arrtwo=getFltwo(93);
$arrmenu=getFlmenu(59);
print_r($arrmenu);


//获取分类树 www.2cto.com
function getTree(){
 global $db;
 $sql="select flid,flpid,flpath,fltitle,fldes,keyword from fenlei order by flid";
 $rs=$db->query($sql);
 $data=array();
 while ($row=$db->fetch_array($rs)){
  $data[]=$row;
 }
 return $data;
}
//活动一级分类 注意在这顶级分类id为 59
function getFlone($flpid){
 global $db;
 $sql="select * from fenlei where flpid={$flpid} order by flid";
 $rs=$db->query($sql);
 $data=array();
 while ($row=$db->fetch_array($rs)){
  $data[]=$row;
 }
 return $data;
}
//活动二级分类
function getFltwo($flpid){
 global $db;
 $sql="select * from fenlei where flpid={$flpid} order by flid";
 $rs=$db->query($sql);
 $data=array();
 while ($row=$db->fetch_array($rs)){
  $data[]=$row;
 }
 return $data;
}
//循环获取菜单,此数据库顶级父分类id为 59
function getFlmenu($flpid){
 global $db;
 $arr=getFlone($flpid);

 foreach ($arr as &$v) {
  $arrt=getFlone($v['flid']);
  
   foreach ($arrt as &$vl){
    $arrtt=getFlone($vl['flid']);
    $vl['tt']=$arrtt;
   }
   $v['two']=$arrt;
 }
 return $arr;
}

?>

 

[php]

 <br>
<pre class="brush:php;toolbar:false">[html] Array <br>
( <br>
    [0] => Array <br>
        ( <br>
            [0] => 93 <br>
            [flid] => 93 <br>
            [1] => 59 <br>
            [flpid] => 59 <br>
            [2] => 国内新闻 <br>
            [fltitle] => 国内新闻 <br>
            [3] => 0-59 <br>
            [flpath] => 0-59 <br>
            [4] => 国内新闻 <br>
            [fldes] => 国内新闻 <br>
            [5] => 国内新闻 <br>
            [keyword] => 国内新闻 <br>
            [two] => Array <br>
                ( <br>
                    [0] => Array <br>
                        ( <br>
                            [0] => 98 <br>
                            [flid] => 98 <br>
                            [1] => 93 <br>
                            [flpid] => 93 <br>
                            [2] => 时政 <br>
                            [fltitle] => 时政 <br>
                            [3] => 0-59-93 <br>
                            [flpath] => 0-59-93 <br>
                            [4] => 时政 <br>
                            [fldes] => 时政 <br>
                            [5] => 时政 <br>
                            [keyword] => 时政 <br>
                            [tt] => Array <br>
                                ( <br>
                                    [0] => Array <br>
                                        ( <br>
                                            [0] => 102 <br>
                                            [flid] => 102 <br>
                                            [1] => 98 <br>
                                            [flpid] => 98 <br>
                                            [2] => 医疗 <br>
                                            [fltitle] => 医疗 <br>
                                            [3] => 0-59-93-98 <br>
                                            [flpath] => 0-59-93-98 <br>
                                            [4] => 医疗 <br>
                                            [fldes] => 医疗 <br>
                                            [5] => 医疗 <br>
                                            [keyword] => 医疗 <br>
                                        ) <br>
  <br>
                                    [1] => Array <br>
                                        ( <br>
                                            [0] => 103 <br>
                                            [flid] => 103 <br>
                                            [1] => 98 <br>
                                            [flpid] => 98 <br>
                                            [2] => 卫生 <br>
                                            [fltitle] => 卫生 <br>
                                            [3] => 0-59-93-98 <br>
                                            [flpath] => 0-59-93-98 <br>
                                            [4] => 卫生 <br>
                                            [fldes] => 卫生 <br>
                                            [5] => 卫生 <br>
                                            [keyword] => 卫生 <br>
                                        ) <br>
  <br>
                                ) <br>
  <br>
                        ) <br>
  <br>
                    [1] => Array <br>
                        ( <br>
                            [0] => 99 <br>
                            [flid] => 99 <br>
                            [1] => 93 <br>
                            [flpid] => 93 <br>
                            [2] => 民生 <br>
                            [fltitle] => 民生 <br>
                            [3] => 0-59-93 <br>
                            [flpath] => 0-59-93 <br>
                            [4] => 民生 <br>
                            [fldes] => 民生 <br>
                            [5] => 民生 <br>
                            [keyword] => 民生 <br>
                            [tt] => Array <br>
                                ( <br>
                                    [0] => Array <br>
                                        ( <br>
                                            [0] => 104 <br>
                                            [flid] => 104 <br>
                                            [1] => 99 <br>
                                            [flpid] => 99 <br>
                                            [2] => 税费 <br>
                                            [fltitle] => 税费 <br>
                                            [3] => 0-59-93-99 <br>
                                            [flpath] => 0-59-93-99 <br>
                                            [4] => 税费 <br>
                                            [fldes] => 税费 <br>
                                            [5] => 税费 <br>
                                            [keyword] => 税费 <br>
                                        ) <br>
  <br>
                                    [1] => Array <br>
                                        ( <br>
                                            [0] => 105 <br>
                                            [flid] => 105 <br>
                                            [1] => 99 <br>
                                            [flpid] => 99 <br>
                                            [2] => 住房 <br>
                                            [fltitle] => 住房 <br>
                                            [3] => 0-59-93-99 <br>
                                            [flpath] => 0-59-93-99 <br>
                                            [4] => 住房 <br>
                                            [fldes] => 住房 <br>
                                            [5] => 住房 <br>
                                            [keyword] => 住房 <br>
                                        ) <br>
  <br>
                                ) <br>
  <br>
                        ) <br>
  <br>
                ) <br>
  <br>
        ) <br>
  <br>
    [1] => Array <br>
        ( <br>
            [0] => 96 <br>
            [flid] => 96 <br>
            [1] => 59 <br>
            [flpid] => 59 <br>
            [2] => 娱乐新闻 <br>
            [fltitle] => 娱乐新闻 <br>
            [3] => 0-59 <br>
            [flpath] => 0-59 <br>
            [4] => 娱乐新闻 <br>
            [fldes] => 娱乐新闻 <br>
            [5] => 娱乐新闻 <br>
            [keyword] => 娱乐新闻 <br>
            [two] => Array <br>
                ( <br>
                    [0] => Array <br>
                        ( <br>
                            [0] => 100 <br>
                            [flid] => 100 <br>
                            [1] => 96 <br>
                            [flpid] => 96 <br>
                            [2] => 八卦 <br>
                            [fltitle] => 八卦 <br>
                            [3] => 0-59-96 <br>
                            [flpath] => 0-59-96 <br>
                            [4] => 八卦 <br>
                            [fldes] => 八卦 <br>
                            [5] => 八卦 <br>
                            [keyword] => 八卦 <br>
                            [tt] => Array <br>
                                ( <br>
                                ) <br>
  <br>
                        ) <br>
  <br>
                    [1] => Array <br>
                        ( <br>
                            [0] => 101 <br>
                            [flid] => 101 <br>
                            [1] => 96 <br>
                            [flpid] => 96 <br>
                            [2] => 明星 <br>
                            [fltitle] => 明星 <br>
                            [3] => 0-59-96 <br>
                            [flpath] => 0-59-96 <br>
                            [4] => 明星 <br>
                            [fldes] => 明星 <br>
                            [5] => 明星 <br>
                            [keyword] => 明星 <br>
                            [tt] => Array <br>
                                ( <br>
                                ) <br>
  <br>
                        ) <br>
  <br>
                ) <br>
  <br>
        ) <br>
  <br>
    [2] => Array <br>
        ( <br>
            [0] => 97 <br>
            [flid] => 97 <br>
            [1] => 59 <br>
            [flpid] => 59 <br>
            [2] => 国外新闻 <br>
            [fltitle] => 国外新闻 <br>
            [3] => 0-59 <br>
            [flpath] => 0-59 <br>
            [4] => 国外新闻 <br>
            [fldes] => 国外新闻 <br>
            [5] => 国外新闻 <br>
            [keyword] => 国外新闻 <br>
            [two] => Array <br>
                ( <br>
                    [0] => Array <br>
                        ( <br>
                            [0] => 106 <br>
                            [flid] => 106 <br>
                            [1] => 97 <br>
                            [flpid] => 97 <br>
                            [2] => 美国 <br>
                            [fltitle] => 美国 <br>
                            [3] => 0-59-97 <br>
                            [flpath] => 0-59-97 <br>
                            [4] => 美国 <br>
                            [fldes] => 美国 <br>
                            [5] => 美国 <br>
                            [keyword] => 美国 <br>
                            [tt] => Array <br>
                                ( <br>
                                    [0] => Array <br>
                                        ( <br>
                                            [0] => 107 <br>
                                            [flid] => 107 <br>
                                            [1] => 106 <br>
                                            [flpid] => 106 <br>
                                            [2] => 纽约 <br>
                                            [fltitle] => 纽约 <br>
                                            [3] => 0-59-97-106 <br>
                                            [flpath] => 0-59-97-106 <br>
                                            [4] => 纽约 <br>
                                            [fldes] => 纽约 <br>
                                            [5] => 纽约 <br>
                                            [keyword] => 纽约 <br>
                                        ) <br>
  <br>
                                ) <br>
  <br>
                        ) <br>
  <br>
                ) <br>
  <br>
        ) <br>
  <br>
) 
<br>
摘自 chaojie2009的专栏						
<p align="left"></p><div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/478378.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/478378.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">//数据库表 ?php include comm.php; //获取分类菜单 $arr=getTree(); $arrone=getFlone(59); $arrtwo=getFltwo(93); $arrmenu=getFlmenu(59); print_r($arrmenu); //获取分类树...</span>
</div>
<div class="art_confoot"></div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn