TreeTable is a PHP class library plug-in that can display unlimited categories as tables. classification levels are represented as table columns, the total number of rows of classification tables, and the cell display category name. treeTable achieves no through row merge and column merge of cells... treeTable is a PHP class library plug-in that can display unlimited categories as tables. classification levels are represented as table columns, the total number of rows of classification tables, and the cell display category name.
TreeTable achieves an unlimited hierarchy by merging rows and columns of cells.
1. construct an array of ID/PID/NAME. The code is as follows:
Array (* 1 => array ('id' => '1', 'parentid' => 0, 'name' => 'Level 1 topic 1 '), * 2 => array ('id' => '2', 'parentid' => 0, 'name' => 'Level 1 column 2 '), * 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level column 1 '), * 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '), * 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '), * 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Level 3 topic 1 '), * 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-level column 2 ')*)
2. import the TreeTable class library with the following code:
Import ('@. ORG. util. tableTree '); // Thinkphp import method 3. generate the TreeTable HTML code $ treeTable-> init ($ treearr); echo $ treeTable-> get_treetable ();
Note: get_treetable () only produces the table body Department, Please build on your own.
The complete php code is as follows:
Init ($ treearr); * 3. obtain the infinite classification HTML code * echo $ treeTable-> get_treetable (); **/class TreeTable {/*** the two-dimensional array required to generate the 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; /*** initialize TreeTable data * @ param array 2-dimensional array * array (* 1 => array ('id' => '1', 'parentid' => 0, 'name' => 'first-level topic 1'), * 2 => array ('id' => '2', 'parentid' => 0, 'name' => 'first-level column 2 '), * 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'second-level column 1 '), * 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2 '), * 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'second-level column 3 '), * 6 => array ('id' => '6', 'parentid' => 3, 'name' => 'Level 3 topic 1 '), * 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'third-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 axis position $ this-> arr [$ k] ['arrchildid'] = $ this-> get_arrchildid ($ v ['id']); // All subnodes $ 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 (); // The total number of rows $ this-> rows = $ this-> get_rows (); // total number of columns // Sort by arrparentid and ID $ 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 rows merged $ this-> arr [$ k] ['colsize'] = $ v ['child _ bottom_num '] = 0? $ This-> columns-$ v ['column'] + 1: 0; // number of column merges} return $ this-> get_tree_arr ();} /*** get the array **/public function get_tree_arr () {return is_array ($ this-> arr )? $ This-> arr: false;}/*** sorts the array by arrparentid/ID number in sequence. **/public function sort_arr () {// 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 and then by sort, idnumber 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); // Obtain each node level 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 ;}}} // re-calculate the foreach with ID as the key name ($ this-> arr as $ k => $ v) {$ arr [$ v ['id'] = $ v ;}$ this-> arr = $ arr ;} /*** get the parent array * @ param int * @ return array */public function get_par Ent ($ 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-level array * @ 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'] =mymyid) $ newarr [$ id] = $ a ;}} return $ newarr? $ Newarr: false;}/*** get the level of the current node * @ param $ myid current node ID **/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 ;} /*** obtain all the underlying nodes of the current node (nodes without subnodes) number of * @ param $ myid node ID number * @ param $ init static variable loaded for the first time **/public function get_child_count ($ m Yid, $ 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 ;} /*** obtain the IDCs of all subnodes of the node * @ param $ catid node IDCs * @ param $ static initialization of the first loading of init **/public function get_arrchildid ($ myid, $ init = true) {static $ childid; if ($ init) $ childid = ''; if (! Is_array ($ this-> arr) return false; foreach ($ this-> arr as $ id => $) {if ($ a ['parentid'] = $ myid) {$ childid = $ childid? $ Childid. ','. $ a ['id']: $ a ['id']; $ this-> get_arrchildid ($ a ['id'], false);} return $ childid ;} /*** get the id of all parent nodes of the node * @ param $ id node id **/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 ;} /*** obtain the row location of the node * @ param $ myid node ID */public function get_row_location ($ myid) {$ nodearr = $ this-> arr; // obtain the position of the row where each node is located. foreach ($ nodearr as $ key => $ node) {if ($ myid = $ node ['id']) {$ node_row_count = 0; $ arrparentid = explode (',', $ node ['arrparentid']);/ /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 ++ ;}}// number of all current nodes and node levels (rowid_level) smaller than the current node level 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 ;} /*** obtain the table display form of the category (excluding the table header) **/public function get_treetable () {$ table_string = ''; for ($ row = 1; $ row <= $ this-> rows; $ row ++) {$ table_string. = "rt"; Foreach ($ this-> arr as $ v) {if ($ v ['row'] = $ row) {$ rowspan = $ v ['rowsize']? "Rowspan = '{$ v ['rowspan']} '": ''; $ colspan = $ v ['colspan']? "Colspan = '{$ v ['colspan']} '": ''; $ table_string. =" rtt{$ V ['name']}";}}$ Table_string. =" rt";}Return $ table_string ;}}