2 PHP Infinite Class classification instance code _php skill

Source: Internet
Author: User
Tags php programming

This article summarizes two PHP unlimited class implementation code, there is a need to learn friends can refer to.
main idea: first look at the third and fourth lines, the value of the parent class ID (parentid) is 1, representing the subclass of the class that belongs to Id=1, and, a, 22 row because it is a level classification, there is no superior classification, so the parent class ID (parentid) value is 0, representing the primary classification, An infinite level classification is realized by analogy. The final effect is:
├ First grade Category A
├─┴ Level Two Classification a
├─┴ Level Two classification B
├ Class B
then is the program, here with PHP as a descriptive language, can be easily changed to other languages, because the principle is similar, is a recursive just.

 <?php
$dbhost = "localhost";  Database host name
$dbuser = "root";  Database user name
$dbpd = "123456";  Database password
$dbname = "Test";  Database name
mysql_connect ($dbhost, $dbuser, $dbpd);    Connect host
mysql_select_db ($dbname);    Select Database
mysql_query ("SET NAMES ' UTF8 '");
Display_tree ("├", 0);
function Display_tree ($tag, $classid) {
  $result = mysql_query ("
    select * from
    ylmf_class
    WHERE ParentID = ' ". $classid. "'
    ;"
  );
  while ($row = Mysql_fetch_array ($result)) {
    //Indent display node name
 echo $tag. $row [' classname ']. "<br/>";
    Call this function again to display the child nodes of the child node
 display_tree ($tag. " ─┴ ", $row [' id ']);
  }
? >

Show in table
treetable achieves an infinite hierarchy and a better presentation hierarchy by combining row and column merging of cells.
1. Build a id/pid/name array of dynamic data that can be generated later by the database. Tree algorithm please click

Array (
* 1 => Array (' ID ' => ' 1 ', ' ParentID ' =>0, ' name ' => ' one-level column one '),
* 2 => Array (' ID ' => ' 2 ', ' ParentID ' =>0, ' name ' => ' Level II '),
* 3 => Array (' ID ' => ' 3 ', ' ParentID ' =>1, ' name ' => ' Two-class column one '),
* 4 => Array (' ID ' => ' 4 ', ' ParentID ' =>1, ' name ' => ' Level two column two '),
* 5 => Array (' ID ' => ' 5 ', ' ParentID ' =>2, ' Name ' => ' two column three '),
* 6 => Array (' ID ' => ' 6 ', ' ParentID ' =>3, ' name ' => ' three-level column one '),
* 7 => Array (' Id ' => ' 7 ', ' ParentID ' =>3, ' name ' => ' Level three ')

2. Import the Treetable class library.
the code is as follows:

Import (' @.org.util.tabletree '); Thinkphp Import method

3. Generate treetable HTML code

 $treeTable->init ($treearr);
echo $treeTable->get_treetable ();

Note: get_treetable () only produces the table Body Department,<talbe></table> please build it yourself.
Complete code

<?php/** * File name:TreeTable.class.php * Description: Universal table Infinite class * *//** * Table Display Unlimited classification is the form of wireless classification has been shown, better to reflect the classification of the subordinate * Use Method: * 1.
Instantiate category * $treeTable = new treetable (); * 2.
Initializes the taxonomy $treearr must be a multidimensional array containing id,parentid,name fields * $treeTable->init ($treearr); * 3.
Get an infinite assortment of HTML code * echo $treeTable->get_treetable (); * * */class Treetable {/** * 2-D array required to generate a tree structure * @var array/Public $arr = Array (),/** * table column number * @var int/Public $column
s = 0;
/** * Table row number * @var int/Public $rows = 0; /** * Initialization treetable Data * @param array 2-D array (* 1 => array (' ID ' => ' 1 ', ' ParentID ' =>0, ' name ' => ' one-level column), * 2 = > Array (' ID ' => ' 2 ', ' ParentID ' =>0, ' name ' => ' Column II '), * 3 => array (' ID ' => ' 3 ', ' ParentID ' =>1 ', ' name ' = > ' two column A '), * 4 => Array (' ID ' => ' 4 ', ' ParentID ' =>1, ' name ' => ' two column II '), * 5 => Array (' ID ' => ' 5 ', ' ParentID ' =>2, ' name ' => ' Level two column III ', * 6 => Array (' ID ' => ' 6 ', ' ParentID ' =>3 ', ' name ' => ' three-level column one '), * 7 => Array (' ID ' => ' 7 ', ' ParentID ' =>3, ' name ' => ' Level threeColumn 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->a rr[$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 row number $this->rows = $this->get_rows ();
Total columns//sorted 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-&G t;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;
Column Merge number} return $this->get_tree_arr (); /** * Get Array */public function Get_tree_arr () {return Is_array ($this->arr)? $this->arr:false;}/** * Reorder array by arrparentid/id number */public function Sort_arr () {///To sort the field foreach ($this->arr as $k => $v) {$order _pid_arr[$k] = $v [' Arrparentid ']; $order _is
cost[] = $v [' sort '];
$order _id_arr[$k] = $v [' id ']; ///First sort according to Arrparentid, then sorted 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); Gets each node hierarchy for ($column = 1; $column <= $this->columns; $column + +) {$row _level = 0; foreach ($this->arr as $ke
Y => $node) {if ($node [' column '] = = $column) {$row _level++; $this->arr[$key] [' column_level '] = $row _level;}}}
Recalculate with ID as the key for foreach ($this->arr as $k => $v) {$arr [$v [' id ']] = $v;} $this->arr = $arr; /** * Gets the parent series * @param int * @return Array/Public function get_parent ($myid) {$newarr = array (); if (!isset ($this->a
rr[$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;
Urn $newarr; /** * Gets the subgroup 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 of the current node * @param $myid the current Node ID number * */Public Function get_level ($myid, $init = True) {static $level = 1; if ($ini
T) $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_coun T ($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 node ID number * @param $catid Node ID number * @param $init the first load initializes the case static */Public function Get_arrchildid ($myid, $in  it = 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_arrchi
Ldid ($a [' ID '], false);
} return $childid; /** * Gets all the parent node ID number * @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 = ex
Plode (', ', $node [' Arrparentid ']); Element foreach ($arrparentid as $pid) {foreach ($nodearr as $node _row) {if ($node _row[' column ') = = = All parent node is less than the current node level is equal to 0 of the underlying node $no dearr[$pid] [' column '] && $nodearr [$pid] [' Column_level '] > $node _row[' column_level '] && $node _row[
' Child_bottom_num '] = = 0) {$node _row_count + +;}} All current node 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 '] & amp;& $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_bo
Ttom_num '] = = 0 {$rows + +;//Total 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 '];
Total columns} return $columns; /** * Gets the form representation of the category (does not include header) */Public Function get_treetable () {$table _string = '; for ($row = 1; $row <= $this->ro ws  $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&LT;TD {$rowspan} {$colspan}> {$v [' name ']} </td>";
}} $table _string. = "Rt</tr>";
return $table _string; }}?>

Hopefully this article will help you learn about PHP programming.

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.