2 PHP Unlimited class instance code, 2 PHP Unlimited instance _php tutorial

Source: Internet
Author: User
Tags learn php programming

2 PHP Unlimited class instance code, 2 PHP Unlimited instances


This article summarizes the two PHP Unlimited class implementation program code, there is a need to learn friends can refer to.
main idea: first look at the third row and the fourth row, the parent class ID (parentid) value is 1, representing the subclass belonging to the Id=1 class, whereas, one, 22 rows because it is a first class classification, there is no superior classification, so the parent class ID (parentid) value is 0, represents the primary classification, An infinite class classification is achieved by analogy. The end result is:
├ First Class Category A
├─┴ Level Two Category A
├─┴ Level Two classification B
├ First Class Category B
Then is the program, here with PHP as the description language, can be easily changed to other languages, because the principle is similar, is a recursive only.

<?php$dbhost = "localhost";  Database hostname $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 '");d isplay_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 ']. "
"; Call this function again to display the child node of the child node Display_tree ($tag. ─┴ ", $row [' id ']);} }? >

Show in table
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

Array (* 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' a '), * 2 = = Array (' id ' = ' 2 ', ' parentid ' = 0, ' name ' = ' column two '), * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two '), * 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 '), * 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>3, ' name ' = = ' Level three column two ') *)

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, please build it yourself.
Full code

<?php/*** File name:treetable.class.php* Description: Universal table Infinite Class classification * *//*** table Display Infinite classification is the form of wireless classification has been shown, better to reflect the classification of the relationship * How to use: * 1. Instantiation Classification * $treeTable = new treetable (); * 2. Initialize the taxonomy, $treearr must be a multidimensional array and contain the Id,parentid,name field * $treeTable->init ($treearr); * 3. Get unlimited category HTML code * echo $treeTable->get_treetable (); * */class treetable {/*** 2-dimensional array required to generate a tree structure * @var array*/public $arr = arr Ay ();/*** Number of table columns * @var int*/public $columns = 0;/*** Table rows * @var int*/public $rows = 0;/*** Initialize treetable data * @param array 2-D arrays * Array (* 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' a '), * 2 = = Array (' id ' = ' 2 ', ' parentid ' = >0, ' name ' = ' A ' column two '), * 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' two ' column one '), * 4 = = Array (' id ' = > ' 4 ', ' ParentID ' =>1, ' name ' = + ' two-level column two '), * 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' two '), * 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' three column one '), * 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>3, ' name ' = ' 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 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 ']; Row merge number $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;} /*** arrparentid/id to reorder the array * */public function Sort_arr () {//field to sort 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, then by sort, 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 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 with ID as key name foreach ($this->arr as $k + = $v) {$arr [$v [' id ']] = $v;} $this->arr = $arr;} /*** Gets the 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;} /*** Gets 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;} /*** 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) $leve L = 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 time the load will be 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 ($c Hildarr = $this->get_child ($myid)) {foreach ($childarr as $v) {$this->get_child_count ($v [' ID '], false)}} else{$count + +;} return $count;} /*** gets node all child node ID number * @param $catid Node ID number * @param $init the first time the load willThe condition 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 = = $a) {if ($a [' parentid '] = = $myid) {$childid = $childid? $ch Ildid. ', '. $a [' id ']: $a [' id ']; $this->get_arrchildid ($a [' ID '], false);}} return $childid;} /*** gets all the parent node ID number of the node * @param $id Node ID number * */public function Get_arrparentid ($id, $arrparentid = ") {if (!is_array ($this->a RR) 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 where each node is located foreach ($nodearr as $key + $node) {if ($myid = = $node [' id ']) {$node _row_count = 0; $arrparentid = explode (' , ', $node [' Arrparentid ']);//The element that all parent nodes are less than the underlying node at the current node level is 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;} /*** gets the number of rows of the table * */public function get_rows () {$row = 0;foreach ($this->arr as $key + = $node) {if ($node [' Child_bottom_nu M '] = = 0) {$rows + +;//total number of}}return $rows;}  /*** gets the number of columns of the table * */public function Get_columns () {$columns = 0; foreach ($this->arr as $key + = $node) {if ($node [' column '] > $columns) {$columns = $node [' column '];//The total number of columns}}return $columns;} /*** Gets the table representation of the classification (without the 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 [' rowspan ']?" Rowspan= ' {$v [' rowspan ']} ': '; $colspan = $v [' colspan ']? "Colspan= ' {$v [' colspan ']} ': '; $table _string. =" RTT{$v [' name ']}";}} $table _string. = "RT";} return $table _string;}}? >

I hope this article will help you learn PHP programming.

http://www.bkjia.com/PHPjc/1070275.html www.bkjia.com true http://www.bkjia.com/PHPjc/1070275.html techarticle 2 PHP Unlimited class instance code, 2 PHP Unlimited Instances This article summarizes two PHP Unlimited class implementation program code, there is a need to learn friends can refer to. Main ideas: First ...

  • 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.