亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP實(shí)現(xiàn)多級分類生成樹的方法示例

 更新時(shí)間:2017年02月07日 09:30:13   作者:風(fēng)起從容  
這篇文章主要介紹了PHP實(shí)現(xiàn)多級分類生成樹的方法,涉及php+mysql數(shù)據(jù)庫操作及數(shù)組與字符串遍歷、替換、組合等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)多級分類生成樹的方法。分享給大家供大家參考,具體如下:

條件,數(shù)據(jù)庫里分類是按id,fid(父ID)實(shí)現(xiàn)多級分類的!

使用方法:

$sql ="XXXXXXXXXX";   //sql語句
$res = $db->Select($sql);  //執(zhí)行sql
$list=array();
treeList(treeGet($res),$list);  /生成樹
print_r($res);  //打印出來看看!

使用結(jié)果:

┣推薦新聞啊
┃┣國際新聞
┃┣dfffffg
┃┣ttttttt
┃┃┗yyyyy

代碼如下:

/**
* 選擇SQL涵數(shù)
*
* @access public
* @param Array  $field  字段信息,支持涵數(shù)
* @param Array  $table  數(shù)據(jù)庫表
* @param Array  $where  條件
* @return SQL SQL語句
*/
function treeGet($data)
{
  $tmptree=null;
  $tree=$data;
  return treeAddNodeToTree($tmptree,treegetbyuid($tree,0,@$field),$tree);
}
/**
*插入SQL涵數(shù)
*
* @access public
* @param Array  $fieldResult  字段信息,支持涵數(shù)
* @param Array  $table  數(shù)據(jù)庫表
* @return SQL SQL語句
*/
function treeAddNodeToTree($Node,$miniTree,&$source)
{
  if(is_array($miniTree)) {
    foreach($miniTree as $k=>$v)
    {
      if(!count($miniTree[$k]['child']=treeAddNodeToTree($miniTree[$k],treegetbyuid($source,@$v['id']),$source)))
      {
        unset($miniTree[$k]['child']);
        $miniTree[$k]['leaf']=true; //設(shè)置葉結(jié)點(diǎn)
      }
    }
    return $Node['child']=$miniTree;
  }
}
function treegetbyuid(&$stree,$uid)
{
  $dtree=array();
  if(is_array($stree)){
    foreach($stree as $k=>$v)
    {
      if($v['fid']==$uid)
      {
        $mytmp=array();
        $mytmp=$v;
        unset($stree[$k]);
        array_push($dtree,$mytmp);
        $mytmp=null;
      }
    }
  }
  return $dtree;
}
/**
*更新SQL涵數(shù)
*
* @access public
* @param Array  $fieldResult  字段信息,支持涵數(shù)
* @param Array  $table  數(shù)據(jù)庫表
* @param Array  $where  條件
* @return SQL SQL語句
*/
function treeMakeDeep($deep)
{
  $returnValue="";
  for (;$deep;$deep--)
  {
    $returnValue.="┃";
  }
  return $returnValue."┣";
}
function treeList($treeData,&$List)
{
  static $deep=0;
  if(is_array($treeData))
  {
    foreach($treeData as $k=>$v)
    {
      $v['deepValue']=treeMakeDeep($deep);
      $v['deep']=$deep;
      $t=$v;
      unset($t['child']);
      array_push($List,$t);
      if($v['child'])
      {
        ++$deep;
        $optionsNode.=treeList($v['child'],$List);
        $deep--;
      }
    }
    if($lastV=array_pop($List))
    {
      $lastV['deepValue']=str_replace('┣','┗',$lastV['deepValue']);
      array_push($List,$lastV);
    }
  }
}
function treeSelect($tree,$id,$options="child")
{
  switch(strtolower($options))
  {
    case"child":
    $tmpTree=array();
    $deep=-1;
    foreach($tree as $k=>$v)
    {
      if($id==$v['id'])
      {
        array_push($tmpTree,$v);
        $deep=$v['deep'];
      } elseif($deep!=-1)
      {
        if($v['deep']<=$deep)
        {
          break;
        } else
        {
          array_push($tmpTree,$v);
        }
      }
    }
    break;
    case "remove":
    default:
    $tmpTree=array();
    $deep=-1;
    foreach($tree as $k=>$v)
    {
      if($id==$v['id'])
      {
        $deep=$v['deep'];
        continue;
      } elseif($deep!=-1)
      {
        if($v['deep']<=$deep)
        {
          array_push($tmpTree,$v);
          $deep=-1;
        }
        continue;
      }
      array_push($tmpTree,$v);
    }
  }
  return $tmpTree;
}

PS:為方便讀者閱讀源碼,上述代碼使用了在線工具http://tools.jb51.net/code/jb51_php_format進(jìn)行了格式化處理。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論