Table Show Unlimited Class (PHP version) _php instance

Source: Internet
Author: User
Treetable can also display hierarchical architecture in an infinite hierarchy by combining row and column merging of cells.
1. Build an array of id/pid/name and later dynamic data that can be generated from the database. Tree algorithm, click
Copy CodeThe code is as follows:
Array
* 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' first column one '),
* 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' "one column II '),
* 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '),
* 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' two ' column two '),
* 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two ' column three '),
* 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three ' column one '),
* 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' three ' level Column II ')
* )

2. Import the Treetable class library.
Copy CodeThe code is as follows:
Import (' @.org.util.tabletree '); Thinkphp Import method

3. Generate treetable HTML code
Copy CodeThe code is as follows:
$treeTable->init ($treearr);
echo $treeTable->get_treetable ();

Note: get_treetable () only produces the table Body Department, Please build it yourself.
Full code
Copy CodeThe code is as follows:
/**
* 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 Unlimited classification is the form of wireless classification has been shown, better to reflect the classification of the ownership of the relationship
* How to use:
* 1. Instantiating classifications
* $treeTable = new treetable ();
* 2. Initialize classification, $treearr must be a multidimensional array and contain the Id,parentid,name field
* $treeTable->init ($treearr);
* 3. Get unlimited Classified HTML code
* Echo $treeTable->get_treetable ();
* */
Class Treetable {
/**
* 2-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;
/**
* Initialization of treetable data
* @param array 2-D
* Array (
* 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' first column one '),
* 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' "one column II '),
* 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '),
* 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' two ' column two '),
* 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two ' column three '),
* 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three ' column one '),
* 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' three ' level Column II ')
* )
*/
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 number
$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 column merges
}
return $this->get_tree_arr ();
}
/**
* Get Array
* */
Public Function Get_tree_arr () {
Return Is_array ($this->arr)? $this->arr:false;
}
/**
* Re-order the array sequentially by ARRPARENTID/ID number
* */
Public Function Sort_arr () {
The field to be sorted
foreach ($this->arr as $k = + $v) {
er _pid_arr[$k] = $v [' Arrparentid '];
er _iscost[] = $v [' sort '];
er _id_arr[$k] = $v [' id '];
}
Sort by Arrparentid first, then by sort, ID number
Array_multisort (
er _pid_arr, SORT_ASC, sort_string,
er _iscost, Sort_desc, Sort_numeric,
er _id_arr, SORT_ASC, Sort_numeric,
$this->arr);
Get the hierarchy of each node
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 Parent Series 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 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;
}
/**
* Gets the level at which the current node is located
* @param $myid 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 (nodes without child nodes) of the current node
* @param $myid Node ID number
* @param $init First load the condition 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 node all child node ID number
* @param $catid Node ID number
* @param $init First load initializes the condition 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 numbers for 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;
Get the location of the row for each node
foreach ($nodearr as $key = = $node) {
if ($myid = = $node [' id ']) {
$node _row_count = 0;
$arrparentid = Explode (', ', $node [' Arrparentid ']);
All the parent nodes are less than the elements at the bottom 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 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;
}
/**
* Get the number of rows in a 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 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;
}
/**
* Get the table representation of the category (not including 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\t
{$v [' name ']}
";
}
}
$table _string. = "\r\t";
}
return $table _string;
}
}
?>
  • Related Article

    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.