ThinkPHP framework uses list_ to_ Tree to achieve unlimited classification, listing all the node examples

Time:2021-2-20

In this paper, an example of ThinkPHP framework using list is given_ to_ Tree implements infinite level classification to list all nodes. The details are as follows:

list_ to_ Tree is very convenient to use. Please refer to the manual for details. Because I need to list all the nodes at the same time when I use it, so I wrote a recursive function and took it out for your reference.

public function index(){
  Load ('extend '); // load extension method
  $Category=D('Category');
  $list = $category - > order ('sort desc ') - > select(); // sort nodes at the same level
  $list=list_ to_ Tree ($list, 'ID','fid '); // please refer to the manual for detailed parameters
  $list=$this->findChild($list);
  dump($list);
}
protected function findChild($arr){
    static $tree=array();
    foreach ($arr as $key=>$val){
        $tree[]=$val;
        if (isset($val['_child'])){
          $this->findChild($val['_child']);
        }
      }
  return $tree;
}
/**
 *Convert the returned data set to tree
 * @access public
 *@ param array $list data set to convert
 *@ param string $PID parent tag field
 *@ param string $level level tag field
 * @return array
 */
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) {
  //Create tree
  $tree = array();
  if(is_array($list)) {
    //Creating array references based on primary keys
    $refer = array();
    foreach ($list as $key => $data) {
      $refer[$data[$pk]] =& $list[$key];
    }
    foreach ($list as $key => $data) {
      //Judge whether there is parent
      $parentId = $data[$pid];
      if ($root == $parentId) {
        $tree[] =& $list[$key];
      }else{
        if (isset($refer[$parentId])) {
          $parent =& $refer[$parentId];
          $parent[$child][] =& $list[$key];
        }
      }
    }
  }
  return $tree;
}

/**
 *Sort the query result set
 * @access public
 *Query result of @ param array $list
 *@ param string $field sorted field name
 *@ param array $sortby sort type
 *ASC forward sort desc reverse sort NAT natural sort
 * @return array
 */
function list_sort_by($list,$field, $sortby='asc') {
  if(is_array($list)){
    $refer = $resultSet = array();
    foreach ($list as $i => $data)
      $refer[$i] = &$data[$field];
    switch ($sortby) {
      Case 'ASC': // forward sort
        asort($refer);
        break;
      Case 'desc': // reverse sort
        arsort($refer);
        break;
      Case 'NAT': // natural sort
        natcasesort($refer);
        break;
    }
    foreach ( $refer as $key=> $val)
      $resultSet[] = &$list[$key];
    return $resultSet;
  }
  return false;
}

/**
 *Search in data list
 * @access public
 *@ param array $list data list
 *@ param mixed $condition query criteria
 *Support array ('name '= > $value) or name = $value
 * @return array
 */
function list_search($list,$condition) {
  if(is_string($condition))
    parse_str($condition,$condition);
  //Returned result collection
  $resultSet = array();
  foreach ($list as $key=>$data){
    $find  =  false;
    foreach ($condition as $field=>$value){
      if(isset($data[$field])) {
        if(0 === strpos($value,'/')) {
          $find  =  preg_match($value,$data[$field]);
        }elseif($data[$field]==$value){
          $find = true;
        }
      }
    }
    if($find)
      $resultSet[]   =  &$list[$key];
  }
  return $resultSet;
}

For more information about ThinkPHP, interested readers can check out the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, CI (CodeIgniter) framework advanced course, Zend framework introduction course and PHP template technology summary.

I hope this article will be helpful to the design of PHP program based on ThinkPHP framework.