<?php /** * File name:TreeTable.class.php * Author:run.gao 312854458@qq.com date:2012-07-24 23:22 gmt+8 * Description: Universal table Infinite Class classification * */ /** * Table Display infinite classification is the form of wireless classification has been displayed, better to reflect the classification of the ownership of the relationship * Use method: * 1. Instantiate categories * $treeTable = new treetable (); * 2. Initializes the taxonomy, $treearr must be a multidimensional array and contains id,parentid,name fields * $treeTable->init ($treearr); * 3. Get infinite category HTML code * Echo $treeTable->get_treetable (); * */ Class Treetable { /** * 2-D arrays required to generate a tree structure * @var Array */ Public $arr = Array (); /** * Number of table columns * @var int */ Public $columns = 0; /** * Number of table rows * @var int */ Public $rows = 0; /** * Initialization of treetable data * @param array of array 2-D * Array ( * 1 => Array (' ID ' => ' 1 ', ' ParentID ' =>0, ' name ' => ' one-level column one '), * 2 => Array (' ID ' => ' 2 ', ' ParentID ' =>0, ' name ' => ' one-level column II '), * 3 => Array (' ID ' => ' 3 ', ' ParentID ' =>1, ' name ' => ' Level two column one '), * 4 => Array (' ID ' => ' 4 ', ' ParentID ' =>1, ' name ' => ' Level two column two '), * 5 => Array (' ID ' => ' 5 ', ' ParentID ' =>2, ' name ' => ' Level two column '), * 6 => Array (' ID ' => ' 6 ', ' ParentID ' =>3, ' name ' => ' Level three column one '), * 7 => Array (' ID ' => ' 7 ', ' ParentID ' =>3, ' name ' => ' Level three column two ') * ) */ 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-Axis 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 underlying ELEMENT nodes } $this->columns = $this->get_columns (); Total number of rows $this->rows = $this->get_rows (); Total number of columns Sort by Arrparentid and ID numbers $this->sort_arr (); foreach ($this->arr as $k => $v) { $this->arr[$k] [' row '] = $this->get_row_location ($v [' id ']); X Axis Position $this->arr[$k] [' rowspan '] = $v [' Child_bottom_num ']; Number of row merges $this->arr[$k] [' colspan '] = $v [' child_bottom_num '] = = 0? $this->columns-$v [' column '] + 1:0; Number of columns merged } return $this->get_tree_arr (); } /** * Get Array * */ Public Function Get_tree_arr () { Return Is_array ($this->arr)? $this->arr:false; } /** * Reorder the array by arrparentid/id number sequence * */ Public Function Sort_arr () { The field to sort on foreach ($this->arr as $k => $v) { $order _pid_arr[$k] = $v [' Arrparentid ']; $order _iscost[] = $v [' sort ']; $order _id_arr[$k] = $v [' id ']; } Sort by Arrparentid first, then by sort, by ID number Array_multisort ( $order _pid_arr, SORT_ASC, sort_string, $order _iscost, Sort_desc, Sort_numeric, $order _id_arr, SORT_ASC, Sort_numeric, $this->arr); Get each node hierarchy 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 with ID as key name foreach ($this->arr as $k => $v) { $arr [$v [' id ']] = $v; } $this->arr = $arr; } /** * Get the parent progression Group * @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; } /** * Get the Sub series Group * @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; } /** * Get the level at which the current node resides * @param $myid the current node ID number * */ 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 underlying nodes of the current node (nodes without child nodes) * @param $myid Node ID number * @param $init The first load 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; } /** * Get all child node ID number of node * @param $catid Node ID number * @param $init The first load to initialize the situation static * */ 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; } /** * Get all parent Node ID number of this node * @param $id Node ID number * */ 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; } /** * Get node location row location * @param $myid Node ID number */ Public Function Get_row_location ($myid) { $nodearr = $this->arr; Gets the position of the row for each node foreach ($nodearr as $key => $node) { if ($myid = = $node [' id ']) { $node _row_count = 0; $arrparentid = Explode (', ', $node [' Arrparentid ']); Elements of all parent nodes that are less than the underlying nodes of the current node level equal to 0 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 the node hierarchy (rowid_level) is less than the current node hierarchy 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; } /** * Get 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; } /** * Get 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 ']; Total number of columns } } return $columns; } /** * Get the table representation of the category (without header) * */ Public Function get_treetable () { $table _string = '; for ($row = 1; $row <= $this->rows; $row + +) { $table _string. = "Rt<tr>"; foreach ($this->arr as $v) { if ($v [' row '] = = $row) { $rowspan = $v [' rowspan ']? "Rowspan= ' {$v [' rowspan ']} '": "; $colspan = $v [' colspan ']? "Colspan= ' {$v [' colspan ']} '": "; $table _string. = "RTT<TD {$rowspan} {$colspan}> {$v [' name ']} </td> "; } } $table _string. = "Rt</tr>"; } return $table _string; } } ?> |