Home > Backend Development > PHP Tutorial > How to implement multi-level classification spanning tree in PHP

How to implement multi-level classification spanning tree in PHP

墨辰丷
Release: 2023-03-28 07:40:02
Original
1501 people have browsed it

This article mainly introduces the method of implementing multi-level classification spanning tree in PHP, involving PHP mysql database operations and array and string traversal, replacement, combination and other related operation skills. Friends in need can refer to the following

The details are as follows:

Conditions, the classification in the database is based on id, fid (parent ID) to achieve multi-level classification!

Usage method:

$sql ="XXXXXXXXXX";   //sql语句
$res = $db->Select($sql);  //执行sql
$list=array();
treeList(treeGet($res),$list);  /生成树
print_r($res);  //打印出来看看!
Copy after login

Usage result:

┣推荐新闻啊
┃┣国际新闻
┃┣dfffffg
┃┣ttttttt
┃┃┗yyyyy
Copy after login

The code is as follows:

/**
* 选择SQL涵数
*
* @access public
* @param Array  $field  字段信息,支持涵数
* @param Array  $table  数据库表
* @param Array  $where  条件
* @return SQL SQL语句
*/
function treeGet($data)
{
  $tmptree=null;
  $tree=$data;
  return treeAddNodeToTree($tmptree,treegetbyuid($tree,0,@$field),$tree);
}
/**
*插入SQL涵数
*
* @access public
* @param Array  $fieldResult  字段信息,支持涵数
* @param Array  $table  数据库表
* @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; //设置叶结点
      }
    }
    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涵数
*
* @access public
* @param Array  $fieldResult  字段信息,支持涵数
* @param Array  $table  数据库表
* @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[&#39;deep&#39;]<=$deep)
        {
          break;
        } else
        {
          array_push($tmpTree,$v);
        }
      }
    }
    break;
    case "remove":
    default:
    $tmpTree=array();
    $deep=-1;
    foreach($tree as $k=>$v)
    {
      if($id==$v[&#39;id&#39;])
      {
        $deep=$v[&#39;deep&#39;];
        continue;
      } elseif($deep!=-1)
      {
        if($v[&#39;deep&#39;]<=$deep)
        {
          array_push($tmpTree,$v);
          $deep=-1;
        }
        continue;
      }
      array_push($tmpTree,$v);
    }
  }
  return $tmpTree;
}
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Large numberMulti-level classificationHow to design the fastest way to obtain, cache, and search queries for data?

sql Cascade query of multi-level classification

SQL processingMultiple Level classification, the query results are in a tree structure

##

The above is the detailed content of How to implement multi-level classification spanning tree in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template