thinkphp5 uses infinitesimal classification
- 2021-11-29 06:24:53
- OfStack
In this paper, we share the specific code of thinkphp5 using infinitesimal classification for your reference. The specific contents are as follows
1. Firstly, the infinite pole classification is completed according to ordinary recursion
2. Add characters according to the level of the semicolon class, store them in the _ name field of the array again, and finally output them according to the _ name field
/**
* Get tree data
* @param $data Data
* @param $title Field name in database
* @param string $fieldPri Primary key in database id
* @param string $fieldPid Parent in database id
* @return array
*/
static public function tree($data, $title, $fieldPri = 'cid', $fieldPid = 'pid')
{
if (!is_array($data) || empty($data))
return array();
$arr = Data::channelList($data);
foreach ($arr as $k => $v) {
$str = "";
if ($v['_level'] > 2) {
for ($i = 1; $i < $v['_level'] - 1; $i++) {
$str .= " The ";
}
}
if ($v['_level'] != 1) {
$t = $title ? $v[$title] : "";
if (isset($arr[$k + 1]) && $arr[$k + 1]['_level'] >= $arr[$k]['_level']) {
$arr[$k]['_name'] = $str . " - " . $v['_html'] . $t;
} else {
$arr[$k]['_name'] = $str . " Off- " . $v['_html'] . $t;
}
} else {
$arr[$k]['_name'] = $v[$title];
}
}
// Set the primary key to $fieldPri
$data = array();
foreach ($arr as $d) {
$data[$d[$fieldPri]] = $d;
}
return $data;
}
/**
* Get all sub-columns
* @param $data Column data
* @param int $pid Column of operation
* @param string $html Character before column name
* @param string $fieldPri Table primary key
* @param string $fieldPid Father id
* @param int $level Rank
* @return array
*/
static public function channelList($data, $pid = 0, $html = " ", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1)
{
$data = self::_channelList($data, $pid, $html, $fieldPri, $fieldPid, $level);
if (empty($data))
return $data;
foreach ($data as $n => $m) {
if ($m['_level'] == 1)
continue;// End a single loop
$data[$n]['_first'] = false;
$data[$n]['_end'] = false;
if (!isset($data[$n - 1]) || $data[$n - 1]['_level'] != $m['_level']) {
$data[$n]['_first'] = true;
}
if (isset($data[$n + 1]) && $data[$n]['_level'] > $data[$n + 1]['_level']) {
$data[$n]['_end'] = true;
}
}
// Update key Is the column primary key
$category=array();
foreach($data as $d){
$category[$d[$fieldPri]]=$d;
}
return $category;
}
// For only channelList Method uses
/**
* Get all sub-columns
* @param $data Column data
* @param int $pid Column of operation
* @param string $html Character before column name
* @param string $fieldPri Table primary key
* @param string $fieldPid Father id
* @param int $level Rank
* @return array
*/
static private function _channelList($data, $pid = 0, $html = " ", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1)
{
if (empty($data))
return array();
$arr = array();
foreach ($data as $v) {
$id = $v[$fieldPri];
if ($v[$fieldPid] == $pid) {
$v['_level'] = $level;
$v['_html'] = str_repeat($html, $level - 1);
$arr[] = $v;
$tmp = self::_channelList($data, $id, $html, $fieldPri, $fieldPid, $level + 1);
$arr = array_merge($arr, $tmp);//array_merge Combine two arrays into 1 Array of numbers
}
}
return $arr;
}