Home  >  Article  >  Backend Development  >  What is Infinitus Classification

What is Infinitus Classification

不言
不言Original
2018-05-31 11:23:112630browse

To realize Infinitus classification, database table creation is the key.

At least three fields are required in the table structure. If you want to avoid recursionloop, then four fields are required.

1. id, the unique identifier of the current data;

2. typename, the type name;

3. parentid, the id of the parent type of the current type;

4. path, which stores the id of the current type and the ids of all its parent types.

These IDs are separated by "-".

5. When it can be realized through the following sql statement, the information under the same top-level category will be displayed together.

select * from table name where condition order by path;

For example:

A very simple, clear and simple unlimited classification example, with For the indentation effect, you only need to query the data table once, and then recursively traverse the result set. If you want to implement column indentation display in PHP, you can refer to it.

$sql = 'select * from cat order by cat_id desc';
$list = $db->getAll($sql);
$list = getLevelCat($list);
function getLevelCat($catlist, $parent_id='0', $html='   ', $level='0'){
  $arr = array();
  foreach($catlist as $val){
    if($val['parent_id']==$parent_id){
      $val['html'] = str_repeat($html,$level);
      $val['level'] = $level;
      $arr[] = $val;
      $arr = array_merge($arr, getLevelCat($catlist, $val['cat_id'], $html, $level+1));
    }
  }
  return $arr;
}

Just a few lines of code, clearer and easier to use.

Related recommendations:

How to implement Infinitus classification function in PHP

##How to create Infinitus classification tree structure

The above is the detailed content of What is Infinitus Classification. For more information, please follow other related articles on the PHP Chinese website!

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