How can I output the tree results to the output file. The following are files for database, tree output, and effects. thank you!
* ************************* Tb_spliter. SQL ************* *********
-- PhpMyAdmin SQL Dump
-- Version 3.5.4
Http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation date: May 01, 2013
-- Server version: 5.5.18
-- PHP version: 5.4.0RC4
SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO ";
SET time_zone = "+ 00:00 ";
/*! 40101 SET @ OLD_CHARACTER_SET_CLIENT = @ CHARACTER_SET_CLIENT */;
/*! 40101 SET @ OLD_CHARACTER_SET_RESULTS = @ CHARACTER_SET_RESULTS */;
/*! 40101 SET @ OLD_COLLATION_CONNECTION = @ COLLATION_CONNECTION */;
/*! 40101 set names utf8 */;
--
-- Database: 'oa'
--
----------------------------------------------------------
--
-- Table structure 'TB _ spliter'
--
Create table if not exists 'TB _ spliter '(
'Id' int (11) not null AUTO_INCREMENT,
'Spliter _ name' varchar (50) not null,
'Spliter' _ cap 'int (4) not null,
'Spliter _ City' varchar (50) not null,
'Spliter _ top_device 'varchar (50) not null,
'Spliter _ top_cable 'varchar (50) not null,
'Up _ spliter' int (11) not null,
'P _ id' int (6) not null,
'P _ name' varchar (50) not null,
'I _ status' int (1) NOT NULL,
'Compute _ date' datetime not null,
'Project _ name' varchar (50) not null,
'Project _ content' text,
'Table _ type' int (1) not null,
'Relate _ Depart' varchar (100) not null,
Primary key ('id ')
) ENGINE = MyISAM default charset = gb2312 COMMENT = 'optical splitter table' AUTO_INCREMENT = 91;
--
-- The data in the table is stored in 'TB _ spliter'
--
Insert into 'TB _ spliter' ('id', 'spliter _ name', 'spliter' _ cap', 'spliter' _ City', 'spliter _ top_device ', 'spliter _ top_cable', 'Up _ spliter', 'p _ id', 'p _ name', 'I _ state', 'compute _ date ', 'Project _ name', 'Project _ content', 'Table _ type', 'relate _ Depart') VALUES
(81, 'A1', 1, 'xt ', 'A', 'none', 69,217, 'hhh', 0, '2017-04-28 00:24:52 ', '0', '0', 1, 'qd '),
(80, 'A2 ', 1, 'xt', 'A', 'none', 69,217, 'hhh ', 0, '2017-04-28 00:24:52 ', '0', '0', 1, 'qd '),
(79, 'A3 ', 1, 'xt', 'A', 'none', 69,217, 'hhh ', 0, '2017-04-28 00:24:52 ', '0', '0', 1, 'qd '),
(78, 'A4 ', 1, 'xt', 'A', 'none', 69,217, 'hhh ', 0, '2017-04-28 00:24:52 ', '0', '0', 1, 'qd '),
(69, 'A', 1, 'xt ', 'none', 'none', 0,217, 'hhh', 0, '2017-04-28 00:15:20 ', '0', '1', 1, 'qh '),
(82, 'B', 1, 'xt ', 'none', 'none', 0,217, 'hhh', 0, '2017-05-01 10:22:51 ', '0', '1', 3, 'qh '),
(83, 'b1 ', 1, 'xt', 'B', 'none', 82,217, 'hhh ', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(84, 'B2', 1, 'xt ',' B ', 'none', 82,217, 'hhh', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(85, 'b3 ', 1, 'xt', 'B', 'none', 82,217, 'hhh ', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(86, 'B4 ', 1, 'xt', 'B', 'none', 82,217, 'hhh ', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(87, 'b5 ', 1, 'xt', 'B', 'none', 82,217, 'hhh ', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(88, 'b6 ', 1, 'xt', 'B', 'none', 82,217, 'hhh ', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(89, 'b61 ', 1, 'xt', 'B', 'none', 88,217, 'hhh ', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh '),
(90, 'b62', 1, 'xt ',' B ', 'none', 88,217, 'hhh', 0, '2017-05-01 10:23:53 ', '0', '0', 3, 'qh ');
/*! 40101 SET CHARACTER_SET_CLIENT = @ OLD_CHARACTER_SET_CLIENT */;
/*! 40101 SET CHARACTER_SET_RESULTS = @ OLD_CHARACTER_SET_RESULTS */;
/*! 40101 SET COLLATION_CONNECTION = @ OLD_COLLATION_CONNECTION */;
* ********************** Tree. php **********************
$ Filename = 'a.txt ';
$ Fff = iconv ('gb2312', 'utf-8', $ fff );
If (! $ Handle = fopen ($ filename, 'w ')){
Echo "cannot open $ filename ";
Exit;
}
Include "../conn. php ";
$ SQL = "select * from tb_spliter where up_spliter = '0'"; // select all top-level devices
$ Result = mysql_query ($ SQL, $ conn );
$ M = 0;
While ($ rows = mysql_fetch_row ($ result )){
$ Sqlstr1 = "select * from tb_spliter where up_spliter = '". $ rows [0]. "'"; // identify the level-1 device under a top-level device
$ Result1 = mysql_query ($ sqlstr1, $ conn );
$ Num = mysql_num_rows ($ result1 );
If (! $ Num) {// if no level-1 device exists
Echo "\ $ a =". $ rows [1]. "". $ m. "no subordinates
";
} Else {
Echo "\ $ B =". $ rows [1]. "". $ m. "sub-level
";
List_first ($ rows [0], $ m); // list_first polls the first-level device
}
$ M + = 1;
}
Function list_first ($ aa, $ m ){
Include "../conn. php ";
$ SQL = "select * from tb_spliter where up_spliter = '". $ aa. "'"; // list_first polls the first-level device
$ Result = mysql_query ($ SQL, $ conn );
While ($ rows2 = mysql_fetch_row ($ result )){
$ Sqlstr1 = "select * from tb_spliter where up_spliter = '". $ rows2 [0]. "'"; // determines whether a branch device exists under a level-1 device.
$ Result1 = mysql_query ($ sqlstr1, $ conn );
$ Nu = mysql_num_rows ($ result1 );
If (! $ Nu ){
Echo $ rows2 [1]. "". $ m. "No sublevel
";
} Else {
Echo $ rows2 [1]. "". $ m. "sub-level
";
List_first ($ rows2 [0], $ m); // list_first poll the device
}
$ M + = 1;
}
}
?>
* ********************** A.txt (desired result) **********************
A --> a1;
A --> a2;
A --> a3;
A --> a4;
B --> b1;
B --> b2;
B --> b3;
B --> b4;
B --> b5;
B --> b6;
B6 --> b61;
B6 --> b62;
Reply to discussion (solution)
Basic Algorithms
$ SQL = "select id, up_spliter, spliter_name from tb_spliter order by 2, 1"; $ rs = mysql_query ($ SQL); $ res = array (); // result array $ ind = array (); // index array while ($ row = mysql_fetch_assoc ($ rs) {list ($ id, $ pid) = array_values ($ row); $ ind [$ id] = $ row; if (isset ($ ind [$ pid]) $ ind [$ pid] ['child '] [$ id] = & $ ind [$ id]; // construct an index if ($ pid = 0) $ res [$ id] = & $ ind [$ id]; // switch to a stub node Group} print_r ($ res );
Array
(
[69] => Array
(
[Id] => 69
[Up_spliter] => 0
[Spliter_name] =>
[Child] => Array
(
[78] => Array
(
[Id] => 78
[Up_spliter] => 69
[Spliter_name] => a4
)
[79] => Array
(
[Id] => 79
[Up_spliter] => 69
[Spliter_name] => a3
)
[80] => Array
(
[Id] => 80
[Up_spliter] => 69
[Spliter_name] => a2
)
[81] => Array
(
[Id] => 81
[Up_spliter] => 69
[Spliter_name] => a1
)
)
)
[82] => Array
(
[Id] => 82
[Up_spliter] => 0
[Spliter_name] => B
[Child] => Array
(
[83] => Array
(
[Id] => 83
[Up_spliter] => 82
[Spliter_name] => b1
)
[84] => Array
(
[Id] => 84
[Up_spliter] => 82
[Spliter_name] => b2
)
[85] => Array
(
[Id] => 85
[Up_spliter] => 82
[Spliter_name] => b3
)
[86] => Array
(
[Id] => 86
[Up_spliter] => 82
[Spliter_name] => b4
)
[87] => Array
(
[Id] => 87
[Up_spliter] => 82
[Spliter_name] => b5
)
[88] => Array
(
[Id] => 88
[Up_spliter] => 82
[Spliter_name] => b6
[Child] => Array
(
[89] => Array
(
[Id] => 89
[Up_spliter] => 88
[Spliter_name] => b61
)
[90] => Array
(
[Id] => 90
[Up_spliter] => 88
[Spliter_name] => b62
)
)
)
)
)
)
Is a multi-dimensional array
Purpose: Display level
Boss, I tried it for a long time and tried to traverse the array to generate a result like a-a1347a-a22.16....a.txt, but I still can't. I hope you can give me some advice.