Home  >  Article  >  php教程  >  Some applications of recursion (2) Infinite classification

Some applications of recursion (2) Infinite classification

WBOY
WBOYOriginal
2016-08-08 08:49:46788browse

Use recursive method to achieve infinite classification

Usually when I am writing a project, I will implement the corresponding classification superior classification when writing something such as mall classification, or other projects department management’s superior departmentI will generally use Infinite Classify by extreme classification

Step 1: First of all, when designing the data table, if I want to implement Infinitus classification, I usually add an additional field pid to the data table. Let me explain it through a new data table,

(1) Create table:

----------------------------

-- Table structure for pid

----------------------------

DROP TABLE IF EXISTS `pid`;

CREATE TABLE pid (

id tinyint unsigned NOT NULL AUTO_INCREMENT primary key comment 'primary key id',

name varchar(32) NOT NULL,

nickname varchar(32) DEFAULT NULL,

pid tinyint(10) unsigned DEFAULT NULL,

sort mediumint(10) unsigned DEFAULT 50

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(2) Insert data:

----------------------------

-- Records of pid

----------------------------

INSERT INTO `pid` VALUES ('1', 'Technology', '', '0', '50');

INSERT INTO `pid` VALUES ('2', 'Military', '', '0', '50');

INSERT INTO `pid` VALUES ('3', 'human and nature', '', '0', '50');

INSERT INTO `pid` VALUES ('4', 'Gourmet', '', '0', '50');

INSERT INTO `pid` VALUES ('5', 'Artificial Intelligence', '', '1', '50');

INSERT INTO `pid` VALUES ('6', 'robot', '', '5', '50');

INSERT INTO `pid` VALUES ('7', 'drone', '', '5', '50');

INSERT INTO `pid` VALUES ('8', 'Autonomous Car', '', '5', '50');

INSERT INTO `pid` VALUES ('9', 'Military robot', 'Haha', '6', '50');

INSERT INTO `pid` VALUES ('10', 'Service Robot', '', '6', '50');

INSERT INTO `pid` VALUES ('11', 'aircraft carrier', '', '2', '50');

INSERT INTO `pid` VALUES ('12', 'carrier-based aircraft', '', '2', '50');

INSERT INTO `pid` VALUES ('13', 'Early Warning Aircraft', '', '2', '50');

INSERT INTO `pid` VALUES ('14', 'missile', '', '2', '50');

INSERT INTO `pid` VALUES ('15', 'A Bite of China', '', '3', '50');

INSERT INTO `pid` VALUES ('16', 'Sichuan cuisine', '', '15', '50');

INSERT INTO `pid` VALUES ('17', 'Cantonese', '', '15', '50');

INSERT INTO `pid` VALUES ('18', 'Hunan cuisine', '', '15', '50');

INSERT INTO `pid` VALUES ('19', 'bio', '', '4', '50');

INSERT INTO `pid` VALUES ('20', 'animal', '', '19', '50');

INSERT INTO `pid` VALUES ('21', 'Plant', '', '19', '50');

INSERT INTO `pid` VALUES ('26', 'Haha', 'Haha', '0', '50');

INSERT INTO `pid` VALUES ('27', 'Hey hey hey', 'Hey hey hey', '26', '50');

INSERT INTO `pid` VALUES ('28', 'Pssssss', 'ssssssss', '26', '50');

Data Sheet:

The data sheet looks like this.

Step 2: Get to the point, Infinitus classification

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">设置字符集</span>
header(<span style="color: #800000;">'</span><span style="color: #800000;">content-type:text/html;charset=utf-8</span><span style="color: #800000;">'</span><span style="color: #000000;">);

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 无限极分类
 * @param   $list array()
 * return array
 </span><span style="color: #008000;">*/</span>
<span style="color: #008000;">//</span><span style="color: #008000;">无限极分类,实现具有父子关系的数据分类</span>
function category($arr,$pid=<span style="color: #800080;">0</span>,$level=<span style="color: #800080;">0</span><span style="color: #000000;">){
    </span><span style="color: #008000;">//</span><span style="color: #008000;">定义一个静态变量,存储一个空数组,用静态变量,是因为静态变量不会被销毁,会保存之前保留的值,普通变量在函数结束时,会死亡,生长周期函数开始到函数结束,再次调用重新开始生长
    </span><span style="color: #008000;">//</span><span style="color: #008000;">保存一个空数组</span>
    <span style="color: #0000ff;">static</span> $list=<span style="color: #000000;">array();
    </span><span style="color: #008000;">//</span><span style="color: #008000;">通过遍历查找是否属于顶级父类,pid=0为顶级父类,</span>
    <span style="color: #0000ff;">foreach</span>($arr <span style="color: #0000ff;">as</span><span style="color: #000000;"> $value){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">进行判断如果pid=0,那么为顶级父类,放入定义的空数组里</span>
        <span style="color: #0000ff;">if</span>($value[<span style="color: #800000;">'</span><span style="color: #800000;">pid</span><span style="color: #800000;">'</span>]==<span style="color: #000000;">$pid){
            </span><span style="color: #008000;">//</span><span style="color: #008000;">添加空格进行分层</span>
            $arr[<span style="color: #800000;">'</span><span style="color: #800000;">level</span><span style="color: #800000;">'</span>]=<span style="color: #000000;">$level;
            $list[]</span>=<span style="color: #000000;">$value;
            </span><span style="color: #008000;">//</span><span style="color: #008000;">递归点,调用自身,把顶级父类的主键id作为父类进行再调用循环,空格+1</span>
            category($arr,$value[<span style="color: #800000;">'</span><span style="color: #800000;">id</span><span style="color: #800000;">'</span>],$level+<span style="color: #800080;">1</span><span style="color: #000000;">);
        }
    }
    </span><span style="color: #0000ff;">return</span> $list;<span style="color: #008000;">//</span><span style="color: #008000;">递归出口</span>
}

Connect data:

Effect:

2. A small extension:

This is just my own understanding of the Infinitus classification and some summary of my own knowledge points. If there is something wrong, I hope you can give me some suggestions, learn together, and make progress together. thanks~

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