mysql and php infinite classification data classification output

Keywords: PHP less

First of all: the data integration part refers to the online blog; but the data output is written by itself, using recursion;

Data classification and interpretation: use PID (child level) = ID (parent level); let the child loop to find out where the parent level is, and then add it;

Recursion: we need to pay attention to the problem of flag(|). The next parent brother needs one less flag(|)

Data table structure:

CREATE TABLE `NewTable` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`pid`  int(10) UNSIGNED NOT NULL ,
`name`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' ,
PRIMARY KEY (`id`),
INDEX `pid` (`pid`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=latin1 COLLATE=latin1_swedish_ci
AUTO_INCREMENT=1
ROW_FORMAT=COMPACT;

Source data

INSERT INTO `test1` VALUES (1, 0, 'menu1');
INSERT INTO `test1` VALUES (2, 0, 'menu2');
INSERT INTO `test1` VALUES (3, 0, 'menu3');
INSERT INTO `test1` VALUES (4, 0, 'menu4');
INSERT INTO `test1` VALUES (5, 0, 'menu5');
INSERT INTO `test1` VALUES (6, 0, 'menu6');
INSERT INTO `test1` VALUES (7, 1, 'menu1-1');
INSERT INTO `test1` VALUES (8, 1, 'menu1-2');
INSERT INTO `test1` VALUES (9, 1, 'menu1-3');
INSERT INTO `test1` VALUES (10, 7, 'menu7-1');
INSERT INTO `test1` VALUES (11, 7, 'menu7-2');
INSERT INTO `test1` VALUES (12, 7, 'menu7-3');
INSERT INTO `test1` VALUES (13, 7, 'menu7-4');
INSERT INTO `test1` VALUES (14, 10, 'menu10-1');
INSERT INTO `test1` VALUES (15, 10, 'menu10-2');
INSERT INTO `test1` VALUES (16, 10, 'menu10-3');
INSERT INTO `test1` VALUES (17, 8, 'menu8-1');

php code: Based on TP5 framework

<?php

namespace app\index\controller;

use app\index\model;
use think\Db;
use think\Debug;

class Index 
{
    public function index()
    {
        $res = Db::table('test1')->select();

        //New array: its id is used as the key for the following operations
        $tree = array();
        foreach ($res as $key => $val) {
            $tree[$val['id']] = $val;
            $tree[$val['id']]['children'] = array();
        }

        //Integrate the element with pid into the corresponding pid=id (this step is the most important)
        foreach ($tree as $key => $val) {
            if ($val['pid'] != 0) {
                $tree[$val['pid']]['children'][] = &$tree[$key];//$val['pid']=$key; equivalent to: pid=id
            }
        }

        //Remove the pid elements in the tree, because they have been classified;
        foreach ($tree as $key => $val) {
            if ($val['pid']==0) continue; 
            unset($tree[$key]);
        }

        $temp = array();
        $grup = '';
        $flag = '|--';


        $this->recursive($tree,$temp,$grup,$flag);
        var_dump($temp);exit;

    }

    public function recursive($data,&$temp,&$division,$flag)
    {
        $diviT = $division;
        if(reset($data)['pid']!=0) $division .= $flag;

        foreach ($data as $key => $val) {
            $temp[] = $division.$val['name'];
            if (is_array($val['children'])) {
                $this->recursive($val['children'],$temp,$division,$flag);
            }
        }

        $division = $diviT;
        return false;

    }

}

The new array output is as follows:

array(17) {
  [0] => string(5) "menu1"
  [1] => string(10) "|--menu1-1"
  [2] => string(13) "|--|--menu7-1"
  [3] => string(17) "|--|--|--menu10-1"
  [4] => string(17) "|--|--|--menu10-2"
  [5] => string(17) "|--|--|--menu10-3"
  [6] => string(13) "|--|--menu7-2"
  [7] => string(13) "|--|--menu7-3"
  [8] => string(13) "|--|--menu7-4"
  [9] => string(10) "|--menu1-2"
  [10] => string(13) "|--|--menu8-1"
  [11] => string(10) "|--menu1-3"
  [12] => string(5) "menu2"
  [13] => string(5) "menu3"
  [14] => string(5) "menu4"
  [15] => string(5) "menu5"
  [16] => string(5) "menu6"
}

Posted by rob.weaver on Thu, 30 Apr 2020 09:56:18 -0700