Two PHP unlimited classification instance codes and two php unlimited instances. Two PHP unlimited classification instance codes and two php unlimited instances This article summarizes two PHP unlimited classification implementation program codes. For more information, see. Main idea: the first two PHP unlimited classification instance codes and two php unlimited instances
This article summarizes two PHP unlimited classification implementation program codes. For more information, see.
Main ideas:First, let's look at row 3 and row 4. the value of the parent class ID (PARENTID) is 1, indicating that it belongs to the subclass of the class id = 1, because the two rows are classified as level-1, and there is no parent category, the value of the parent category ID (PARENTID) is 0, indicating that the primary category, and so on, implements an infinite classification. The final result is:
Category level 1
Category-two level
─ ── Second-level classification B
Category 1 Class B
Then there is the program. here, PHP is used as the description language, which can be easily changed to other languages. because the principle is similar, it is just a recursion.
<? Php $ dbhost = "localhost"; // database host name $ dbuser = "root"; // database username $ dbpd = "123456 "; // database password $ dbname = "test"; // database name mysql_connect ($ dbhost, $ dbuser, $ dbpd); // connect to the host mysql_select_db ($ dbname ); // select the database mysql_query ("set names 'utf8'"); display_tree ("success", 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 subnode display_tree ($ tag."-success ", $ row ['id']); }}?>
Show in table
TreeTable achieves an unlimited 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
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
$treeTable->init($treearr);echo $treeTable->get_treetable();
Note: get_treetable () only produces the table body Department, Build it on your own.
Complete Code
<? Php/*** File name: TreeTable. class. php * Description: a common table of unlimited classification ** // ** table of infinite classification is displayed in the form of a wireless classification table, better reflect the relationship of categories * usage: * 1. instantiation class * $ treeTable = new TreeTable (); * 2. initialize the category. $ treearr must be a multi-dimensional array containing the id, parentid, name field * $ treeTable-> 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 $ column S = 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' => 'Level 1 column 2'), * 3 => array ('id' => '3', 'parentid' => 1, 'name' => 'column 1 '), * 4 => array ('id' => '4', 'parentid' => 1, 'name' => 'second-level column 2'), * 5 => array ('id' => '5', 'parentid' => 2, 'name' => 'column 3 '), * 6 => array ('id' => '6', 'parentid' => 3, 'Na Me '=> 'Level 3 topic 1'), * 7 => array ('id' => '7', 'parentid' => 3, 'name' => 'Level 3 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 layers Element node} $ 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 ($ sort, SORT_ASC, SORT_STRING, $ order_iscost, SORT_DESC, SORT_NUMERIC, $ order_id_arr, SORT_ASC, SORT_NUMERIC, $ this-> arr); // Get Take 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_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 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 ['parenti D'] ==$ myid) $ 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 nodes * @ param $ myid node ID number * @ param $ init static variable loaded for the first time **/public function get_child_count ($ myid, $ init = true) {s Tatic $ 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']); // all parent nodes are less than the current node level 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 ['c Olumn_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 ;}}?>
I hope this article will help you learn php programming.
This article summarizes two PHP unlimited classification implementation program codes. For more information, see. Main idea: first...