Table showing unlimited levels of classification of PHP edition
- 2020-05-19 04:27:38
- OfStack
TreeTable also demonstrates a hierarchical architecture by implementing an infinite hierarchy of cells by combining rows and columns.
1. Build an array of ID/PID/NAME, and generate dynamic data through the database at a later stage. Click on the Tree algorithm
2. Import the TreeTable class library.
3. Generate TreeTable HTML code
Note: get_treetable() only produces table departments, < TALBE > < /TABLE > Please build it yourself.
The complete code
1. Build an array of ID/PID/NAME, and generate dynamic data through the database at a later stage. Click on the Tree algorithm
array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'1 Level column 1'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'1 Level column 2'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'2 Level column 1'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'2 Level column 2'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'2 Level column 3'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'3 Level column 1'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'3 Level column 2')
* )
2. Import the TreeTable class library.
import('@.ORG.Util.TableTree'); //Thinkphp The import method
3. Generate TreeTable HTML code
$treeTable->init($treearr);
echo $treeTable->get_treetable();
Note: get_treetable() only produces table departments, < TALBE > < /TABLE > Please build it yourself.
The complete code
<?php
/**
* File name: TreeTable.class.php
* Author: run.gao 312854458@qq.com Date: 2012-07-24 23:22 GMT+8
* Description: Universal table unlimited classification
* */
/**
* Table display infinite classification is to show the form of wireless classification table, which can better reflect the ownership relationship of classification
* Usage:
* 1. Instantiated classification
* $treeTable = new TreeTable();
* 2. Class initialization, $treearr It must be 1 Two multidimensional arrays and contains id,parentid,name field
* $treeTable->init($treearr);
* 3. Get infinite classification HTML code
* echo $treeTable->get_treetable();
* */
class TreeTable {
/**
* What is needed to generate a tree structure 2 Dimensional array
* @var array
*/
public $arr = array();
/**
* Form the number of columns
* @var int
*/
public $columns = 0;
/**
* Table rows
* @var int
*/
public $rows = 0;
/**
* Initialize the TreeTable data
* @param array 2 Dimensional array
* array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'1 Level column 1'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'1 Level column 2'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'2 Level column 1'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'2 Level column 2'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'2 Level column 3'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'3 Level column 1'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'3 Level column 2')
* )
*/
public function init($arr=array()){
if(!is_array($arr)) return false;
foreach ($arr as $k=>$v) {
$this->arr[$v['id']] = $v;
}
foreach ($this->arr as $k => $v){
$this->arr[$k]['column'] = $this->get_level($v['id']); // Y Shaft position
$this->arr[$k]['arrchildid'] = $this->get_arrchildid($v['id']); // All child nodes
$this->arr[$k]['arrparentid'] = $this->get_arrparentid($v['id']); // All parent nodes
$this->arr[$k]['child_bottom_num'] = $this->get_child_count($v['id']); // All the underlying element nodes
}
$this->columns = $this->get_columns(); // Total number of rows
$this->rows = $this->get_rows(); // The total number of columns
// In accordance with the arrparentid and id Number sort
$this->sort_arr();
foreach ($this->arr as $k => $v){
$this->arr[$k]['row'] = $this->get_row_location($v['id']); // X Shaft position
$this->arr[$k]['rowspan'] = $v['child_bottom_num']; // Number of merger
$this->arr[$k]['colspan'] = $v['child_bottom_num'] == 0 ? $this->columns - $v['column'] + 1 : 0; // Number of columns
}
return $this->get_tree_arr();
}
/**
* Access to an array
* */
public function get_tree_arr(){
return is_array($this->arr) ? $this->arr : false;
}
/**
* According to the arrparentid/id Reorder the array in turn
* */
public function sort_arr(){
// The field to sort
foreach ($this->arr as $k => $v){
$order_pid_arr[$k] = $v['arrparentid'];
$order_iscost[] = $v['sort'];
$order_id_arr[$k] = $v['id'];
}
// According to the first arrparentid Sort by, sort by ,id The sorting
array_multisort(
$order_pid_arr, SORT_ASC, SORT_STRING,
$order_iscost, SORT_DESC, SORT_NUMERIC,
$order_id_arr, SORT_ASC, SORT_NUMERIC,
$this->arr);
// For each 1 Hierarchy of nodes
for ($column = 1; $column <= $this->columns; $column++) {
$row_level = 0;
foreach ($this->arr as $key => $node){
if ($node['column'] == $column){
$row_level++;
$this->arr[$key]['column_level'] = $row_level;
}
}
}
// Recalculate to ID As the key,
foreach ($this->arr as $k=>$v) {
$arr[$v['id']] = $v;
}
$this->arr = $arr;
}
/**
* I get the parent series
* @param int
* @return array
*/
public function get_parent($myid){
$newarr = array();
if(!isset($this->arr[$myid])) return false;
$pid = $this->arr[$myid]['parentid'];
$pid = $this->arr[$pid]['parentid'];
if(is_array($this->arr)){
foreach($this->arr as $id => $a){
if($a['parentid'] == $pid) $newarr[$id] = $a;
}
}
return $newarr;
}
/**
* I get the subseries
* @param int
* @return array
*/
public function get_child($myid){
$a = $newarr = array();
if(is_array($this->arr)){
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) $newarr[$id] = $a;
}
}
return $newarr ? $newarr : false;
}
/**
* Gets the level of the current node
* @param $myid The current node ID No.
* */
public function get_level($myid, $init = true){
static $level = 1;
if($init) $level = 1;
if ($this->arr[$myid]['parentid']) {
$level++;
$this->get_level($this->arr[$myid]['parentid'], false);
}
return $level;
}
/**
* Gets the number of all the underlying nodes (nodes without children) of the current node
* @param $myid node ID No.
* @param $init The first 1 Secondary loading will be the case static variable
* */
public function get_child_count($myid, $init = true){
static $count = 0;
if($init) $count = 0;
if(!$this->get_child($myid) && $init) return 0;
if($childarr = $this->get_child($myid)){
foreach ($childarr as $v){
$this->get_child_count($v['id'], false);
}
}else{
$count++;
}
return $count;
}
/**
* Gets all child nodes of the node ID No.
* @param $catid node ID No.
* @param $init The first 1 Secondary loading will be the case static Initialize the
* */
public function get_arrchildid($myid, $init = true) {
static $childid;
if($init) $childid = '';
if(!is_array($this->arr)) return false;
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) {
$childid = $childid ? $childid.','.$a['id'] : $a['id'];
$this->get_arrchildid($a['id'], false);
}
}
return $childid ;
}
/**
* Gets all the parents of the node ID No.
* @param $id node ID No.
* */
public function get_arrparentid($id, $arrparentid = '') {
if(!is_array($this->arr)) return false;
$parentid = $this->arr[$id]['parentid'];
if($parentid > 0) $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
if($parentid) $arrparentid = $this->get_arrparentid($parentid, $arrparentid);
return $arrparentid;
}
/**
* Gets the row location of the node location
* @param $myid node ID No.
*/
public function get_row_location($myid){
$nodearr = $this->arr;
// For each 1 The position of the row of nodes
foreach ($nodearr as $key => $node){
if($myid == $node['id']) {
$node_row_count = 0;
$arrparentid = explode(',', $node['arrparentid']);
// All parent nodes are smaller than the bottom node of the current node hierarchy is equal to 0 The elements of the
foreach ($arrparentid as $pid){
foreach ($nodearr as $node_row){
if($node_row['column'] == $nodearr[$pid]['column'] && $nodearr[$pid]['column_level'] > $node_row['column_level'] && $node_row['child_bottom_num'] == 0){
$node_row_count ++;
}
}
}
// All current nodes and node hierarchy ( rowid_level ) is less than the number of current node levels
foreach ($nodearr as $node_row){
if($node['column'] == $node_row['column'] && $node_row['column_level'] < $node['column_level']){
$node_row_count += $node_row['child_bottom_num'] ? $node_row['child_bottom_num'] : 1;
}
}
$node_row_count++;
break;
}
}
return $node_row_count;
}
/**
* Gets the number of rows in the table
* */
public function get_rows(){
$row = 0;
foreach ($this->arr as $key => $node){
if($node['child_bottom_num'] == 0){
$rows++; // Total number of rows
}
}
return $rows;
}
/**
* Gets the number of columns in the table
* */
public function get_columns(){
$columns = 0 ;
foreach ($this->arr as $key => $node){
if($node['column'] > $columns){
$columns = $node['column']; // The total number of columns
}
}
return $columns;
}
/**
* Gets a table representation of the categories ( Does not include header )
* */
public function get_treetable(){
$table_string = '';
for($row = 1; $row <= $this->rows; $row++){
$table_string .= "\r\t<tr>";
foreach ($this->arr as $v){
if($v['row'] == $row){
$rowspan = $v['rowspan'] ? "rowspan='{$v['rowspan']}'" : '';
$colspan = $v['colspan'] ? "colspan='{$v['colspan']}'" : '';
$table_string .= "\r\t\t<td {$rowspan} {$colspan}>
{$v['name']}
</td>";
}
}
$table_string .= "\r\t</tr>";
}
return $table_string;
}
}
?>