Home >Backend Development >PHP Tutorial >PHP unlimited classification can support the output of tree diagram implementation code_PHP tutorial

PHP unlimited classification can support the output of tree diagram implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:15842browse

This is a piece of PHP unlimited classification implementation code that can support the output of tree diagrams. Friends who need to know more about it can refer to it from other websites. For the database structure, we only need the three fields of id, parentid, and name. Everyone You can create it yourself.

We only need the database structure

No platform restrictions
Just tell the id, parentid, name

The following is the php code, which requires php environment support

The code is as follows Copy code

/**
* Universal tree class, can generate any tree structure
*/
class tree
{
/**
* The 2-dimensional array required to generate a tree structure
* @var array
​*/
var $arr = array();

/**
* The modification symbols required to generate a tree structure can be replaced by pictures
* @var array
​*/
var $icon = array('│','├','└');

/**
 * @access private
 */
var $ret = '';

/**
* Constructor, initialization class
* @param array 2-dimensional array, for example:
* array(
*   1 => array('id'=>'1','parentid'=>0,'name'=>'First-level column one'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'First-level column two'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'Second-level column one'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'Second-level column two'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'Second-level column three'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'Third-level column one'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'Third-level column two')
* )
​*/
function tree($arr=array())
{
$this->arr = $arr;
$this->ret = '';
Return is_array($arr);
}

/**
* Get the parent array
* @param int
* @return array
​*/
function get_parent($myid)
{
$newarr = array();
if(!isset($this->arr[$myid])) return false;
$pid = $this->arr[$myid]['parentid'];
$pid = $this->arr[$pid]['parentid'];
if(is_array($this->arr))
{
foreach($this->arr as $id => $a)
{
If($a['parentid'] == $pid) $newarr[$id] = $a;
}
}
return $newarr;
}

/**
* Get the child array
* @param int
* @return array
​*/
function get_child($myid)
{
$a = $newarr = array();
if(is_array($this->arr))
{
foreach($this->arr as $id => $a)
{
If($a['parentid'] == $myid) $newarr[$id] = $a;
}
}
return $newarr ? $newarr : false;
}

/**
* Get the current position array
* @param int
* @return array
​*/
function get_pos($myid,&$newarr)
{
$a = array();
if(!isset($this->arr[$myid])) return false;
$newarr[] = $this->arr[$myid];
$pid = $this->arr[$myid]['parentid'];
if(isset($this->arr[$pid]))
{
$this->get_pos($pid,$newarr);
}
if(is_array($newarr))
{
krsort($newarr);
foreach($newarr as $v)
{
$a[$v['id']] = $v;
}
}
return $a;
}


 /**
*---------------------------------------------
* Get tree structure
*---------------------------------------------
* @author Midnight(Yang Yunzhou), yangyunzhou@foxmail.com
* @param $myid means to get all children under this ID
* @param $str Generate basic code for tree structure, for example: ""
* @param $sid The selected ID, for example, you need to use it when making a tree drop-down box
* @param $adds
* @param $str_group
* @return unknown_type
​*/
 function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
 {
  $number=1;
  $child = $this->get_child($myid);
  if(is_array($child))
  {
      $total = sqlserver/42852.htm target=_blank >count($child);
   foreach($child as $id=>$a)
   {
    $j=$k='';
    if($number==$total)
    {
     $j .= $this->icon[2];
    }
    else
    {
     $j .= $this->icon[1];
     $k = $adds ? $this->icon[0] : '';
    }
    $spacer = $adds ? $adds.$j : '';
    $selected = $id==$sid ? 'selected' : '';
    @extract($a);
    $parentid == 0 && $str_group ? eval("$nstr = "$str_group";") : eval("$nstr = "$str";");
    $this->ret .= $nstr;
    $this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
    $number++;
   }
  }
  return $this->ret;
 }
    /**
*Similar to the previous method, but allows multiple selections
​*/
 function get_tree_multi($myid, $str, $sid = 0, $adds = '')
 {
  $number=1;
  $child = $this->get_child($myid);
  if(is_array($child))
  {
      $total = count($child);
   foreach($child as $id=>$a)
   {
    $j=$k='';
    if($number==$total)
    {
     $j .= $this->icon[2];
    }
    else
    {
     $j .= $this->icon[1];
     $k = $adds ? $this->icon[0] : '';
    }
    $spacer = $adds ? $adds.$j : '';

    $selected = $this->have($sid,$id) ? 'selected' : '';
    //echo $sid.'=>'.$id.' : '.$selected.' .
';
    @extract($a);
    eval("$nstr = "$str";");
    $this->ret .= $nstr;
    $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
    $number++;
   }
  }
  return $this->ret;
 }

 function have($list,$item){
  return(strpos(',,'.$list.',',','.$item.','));
 }
}
?>

效果就是

  bbb   ccc
 代码如下
 代码如下 复制代码
aa
  bbb
  ccc
复制代码

aa
这样哦,可实现无限级分类哦,

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631610.htmlTechArticleThis is a PHP unlimited classification implementation code that can support the output of tree diagrams. It is transferred from other websites for those who need to know more. You can refer to it. For the database structure, we only need these three items: id, parentid, and name...
Statement:
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