Table display unlimited classification (PHP version) _ php instances

Source: Internet
Author: User
TreeTable is a PHP class library plug-in that can display infinite categories as tables. The classification level is represented as the column of the table, and the number of rows of the classification is the total number of table categories. The cell Display category name TreeTable provides an unlimited level of display hierarchy by merging rows and columns of cells.
1. Construct an array of ID/PID/NAME, which can be generated by the database later. Click Tree Algorithm

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.

The Code is as follows:


Import ('@. ORG. Util. TableTree'); // Thinkphp import Method


3. Generate the TreeTable HTML code

The Code is as follows:


$ TreeTable-> init ($ treearr );
Echo $ treeTable-> get_treetable ();


Note: get_treetable () only produces the table body department, Build it on your own.
Complete code

The Code is as follows:


/**
* File name: TreeTable. class. php
* Author: run. gao 312854458@qq.com Date: 2012-07-24 GMT + 8
* Description: Generic tables are classified infinitely.
**/
/**
* An unlimited category is displayed in the form of a wireless classification table, which better reflects the relationship of the classification.
* Usage:
* 1. instantiate a category
* $ TreeTable = new TreeTable ();
* 2. initialize the category. $ treearr must be a multi-dimensional array and contain the id, parentid, and name fields.
* $ TreeTable-> init ($ treearr );
* 3. Obtain the infinite classification HTML code
* Echo $ treeTable-> get_treetable ();
**/
Class TreeTable {
/**
* The two-dimensional array 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;
/**
* Initialize TreeTable data
* @ Param array 2-dimensional array
* 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 ')
*)
*/
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 (); // 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] ['colspan '] = $ v ['child _ bottom_num'] = 0? $ This-> columns-$ v ['column '] + 1: 0; // Number of columns merged
}
Return $ this-> get_tree_arr ();
}
/**
* Getting Arrays
**/
Public function get_tree_arr (){
Return is_array ($ this-> arr )? $ This-> arr: false;
}
/**
* Sort the array by arrparentid/ID.
**/
Public function sort_arr (){
// Fields to be sorted
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 and ID
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;
}
}
}
// Recalculate the key name with ID
Foreach ($ this-> arr as $ k => $ v ){
$ Arr [$ v ['id'] = $ v;
}
$ This-> arr = $ arr;
}
/**
* Obtain the parent array.
* @ 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 => $ ){
If ($ a ['parentid'] = $ pid) $ newarr [$ id] = $;
}
}
Return $ newarr;
}
/**
* Obtain the child-level array.
* @ Param int
* @ Return array
*/
Public function get_child ($ myid ){
$ A = $ newarr = array ();
If (is_array ($ this-> arr )){
Foreach ($ this-> arr as $ id => $ ){
If ($ a ['parentid'] ==$ myid) $ newarr [$ id] = $;
}
}
Return $ newarr? $ Newarr: false;
}
/**
* Get the hierarchy of the current node
* @ Param $ myid ID of the current node
**/
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 the number of all underlying nodes (nodes without subnodes) of the current node.
* @ Param $ myid node ID
* @ Param $ static variable when init is loaded for the first time
**/
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;
}
/**
* Obtain the IDs of all subnodes of a node.
* @ Param $ catid node ID
* @ Param $ init the first loading will perform static initialization.
**/
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;
}
/**
* Obtain the IDs 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 row location of each node
Foreach ($ nodearr as $ key => $ node ){
If ($ myid = $ node ['id']) {
$ Node_row_count = 0;
$ Arrparentid = explode (',', $ node ['arrparentid']);
// All elements whose parent nodes are less than or equal to 0 at the underlying node level of the current node
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 whose rowid_level is 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;
}
/**
* Obtain 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;
}
/**
* Obtain the number of columns in a 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. = "\ r \ t";
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
{$ V ['name']}
";
}
}
$ Table_string. = "\ r \ t";
}
Return $ table_string;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.