PHP Realization of Infinite Classification with Recursive Function

  • 2021-12-04 09:32:37
  • OfStack

I believe many small partners who learn php will try to make an online mall as a way to improve their technology. All kinds of commodity classification, trade name and other operations should be handy, so you can try to make an unlimited classification list.

What is an infinite class?

Infinite classification is a kind of classification skill, such as department organization, article classification, subject classification, etc. It is often used to infinite classification, so it is good to simply understand it as classification. In fact, if we think about it carefully, there are simply too many categories in life. Clothes can be divided into men's clothing and women's clothing, tops and trousers, and can also be classified according to age groups. Classification is everywhere, and classification is "infinite". I won't talk about the necessity of infinite classification here.

Brief introduction of infinite classification principle

Infinite classification seems to be "high and high", but in fact the principle is very simple. Infinite classification requires not only the ingenuity of code, but also the rationality of database design. To satisfy the unlimited classification, the database needs to have two necessary fields, id and pid. id is used to identify itself, while pid is used to indicate the parent id. That is to say, each classification record not only describes itself, but also describes another id that is most closely concerned with it. Seemingly complicated things were solved by such a small skill.

php infinite class classification is often used, I have been written directly before 1, so I didn't study it carefully. The following is a simple php infinite class classification function realized by recursion; Maybe this is not the optimal method, but it is enough for a general application.

Data table structure


CREATE TABLE IF NOT EXISTS `category` (
 `id` int(5) NOT NULL AUTO_INCREMENT COMMENT ' Only 1 Self-increasing id',
 `pid` int(5) NOT NULL DEFAULT '0' COMMENT ' Father id',
 `sort` int(2) NOT NULL DEFAULT '0' COMMENT ' Sort number ',
 `name` varchar(30) DEFAULT NULL COMMENT ' Name ',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=' Infinitesimal classification table ' AUTO_INCREMENT=1 ;

Data


INSERT INTO `category` (`id`, `pid`, `sort`, `name`) VALUES
(1, 0, 1, 'php'),
(2, 0, 2, ' Database '),
(3, 0, 3, 'javascript'),
(4, 1, 1, ' Frame template '),
(5, 1, 2, ' Function summary '),
(6, 2, 1, 'mysql'),
(7, 4, 1, ' Framework '),
(8, 4, 2, ' Template '),
(9, 8, 1, 'smarty'),
(10, 7, 2, 'thinkphp'),
(11, 10, 1, 'thinkphp Skill '),
(12, 10, 2, 'thinkphp Template '),
(13, 12, 3, ' Summary of template knowledge '),
(14, 12, 2, ' Template video tutorial '),
(15, 11, 1, 'model Skill ');

Function implementation code


function tree(&$list,$pid=0,$level=0,$html='--'){
  static $tree=array();
  foreach($list as $v){
    if($v['pid']==$pid){
      $v['level']=$level;
      $v['html']=str_repeat($html,$level);
      $tree[]=$v;
      tree($list,$v['id'],$level+1,$html);
    } 
  }
  return $tree;
}

The first parameter of the above tree function, $list, is the result set of a 2-dimensional array as shown in the above table. It should be noted that the sql statement to get the result set from the database must be added order by sort asc Otherwise, the sort field will not function as a sort.

Summarize


Related articles: