How do I assign a value to an array for the data loop of the tree structure queried by the database?

Source: Internet
Author: User
How do I assign a value to an array for the data loop of the tree structure queried by the database? // Database
Id parentid name status
03e782cd-0f00-11e3-bcf6-001d608a0834 head office 1
49d15b3a-1798-11e3-991e-001d608a0834 55ac840c-0f01-11e3-bcf6-001d608a0834 design section 1
49e24a33-1798-11e3-991e-001d608a0834 55ac840c-0f01-11e3-bcf6-001d608a0834 implementation section 1
49f452aa-1798-11e3-991e-001d608a0834 55ac840c-0f01-11e3-bcf6-001d608a0834 QC 1
5599eec4-0f01-11e3-bcf6-001d608a0834 f6597beb-0f00-11e3-bcf6-001d608a0834 Marketing 1
55ac840c-0f01-11e3-bcf6-001d608a0834 f6597beb-0f00-11e3-bcf6-001d608a0834 R & D department 1
55ca0dad-0f01-11e3-bcf6-001d608a0834 f6597beb-0f00-11e3-bcf6-001d608a0834 personnel department 1
B5e86db9-0f01-11e3-bcf6-001d608a0834 f6667833-0f00-11e3-bcf6-001d608a0834 Finance Department 1
B5f69ff5-0f01-11e3-bcf6-001d608a0834 f6667833-0f00-11e3-bcf6-001d608a0834 manager 1
B60bc9b5-0f01-11e3-bcf6-001d608a0834 f6667833-0f00-11e3-bcf6-001d608a0834 logistics department 1
F6597beb-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834 a Company 1
F6667833-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834 two companies 1
F6761c03-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834 three companies 1
F683c3c7-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834 four companies 1
F6955d9f-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834 five companies 1
// Php method code
Public function getTreeModel (){
$ SQL = "select * from menutree where parentid is NULL ";
$ Quer = $ this-> db-> query ($ SQL );

$ Row = $ quer-> result_array ();
$ Data = array ();
If ($ quer-> num_rows ()> 0 ){
// Head office
$ Data = $ this-> getArray ($ row );
}
Echo count ($ data )."
";
Return $ data;
}
// It is a problem to assign values to arrays in this loop... The page outputs only level 1 and Level 3 and level 4, but var_dump ($ row3) has values. Please refer to the following method for more information.
Public function getArray ($ row ){
$ Data = array ();
Foreach ($ row as $ val ){

$ Arr1 ['id'] = $ val ['id'];
$ Arr1 ['menuname'] = $ val ['menuname'];

$ Data [] = $ arr1;

$ Sql2 = "select * from menutree where parentid = '$ val [id]'";
$ Quer2 = $ this-> db-> query ($ sql2 );
$ Row2 = $ quer2-> result_array ();
If ($ quer2-> num_rows ()> 0 ){
Foreach ($ row2 as $ va2 ){
$ Arr2 ['id'] = $ va2 ['id'];
$ Arr2 ['menuname'] = $ va2 ['menuname'];

$ Data [] = $ arr2;

// Echo $ va2 ['menuname']."
";
$ Sql3 = "select * from menutree where parentid = '$ va2 [id]'";
$ Quer3 = $ this-> db-> query ($ sql3 );
$ Row3 = $ quer3-> result_array ();
If ($ quer3-> num_rows ()> 0 ){
$ This-> getArray ($ row3 );
}
}
$ This-> getArray ($ row2 );
}
}
Return $ data;
}

// Php page -- paste the value of a page
For ($ I = 0; $ I Foreach ($ alldata [$ I] as $ key => $ r ){
Echo $ key. "----". $ r ."
";
}
}

// This is the result of the page output.
Id ---- 03e782cd-0f00-11e3-bcf6-001d608a0834
Menuname ---- head office
Id ---- f6597beb-0f00-11e3-bcf6-001d608a0834
Menuname ---- a company
Id ---- f6667833-0f00-11e3-bcf6-001d608a0834
Menuname ---- Company 2
Id ---- f6761c03-0f00-11e3-bcf6-001d608a0834
Menuname ---- three companies
Id ---- f683c3c7-0f00-11e3-bcf6-001d608a0834
Menuname ---- company 4
Id ---- f6955d9f-0f00-11e3-bcf6-001d608a0834
Menuname ---- company 5


Reply to discussion (solution)

No one !!!!!!!!!!!!!!!!!!!

Public function getArray ($ row ){
$ Data = array ();
If (count ($ row)> 0 ){
For ($ I = 0; $ I $ Data [$ I] = $ row [$ I];
$ Id2 = $ row [$ I] ['id'];
$ Sql2 = "select * from menutree where parentid = '$ id2 '";
$ Quer2 = $ this-> db-> query ($ sql2 );
$ Row2 = $ quer2-> result_array ();
// Var_dump ($ quer2-> num_rows ());
If ($ quer2-> num_rows ()> 0 ){
For ($ j = 0; $ j // $ Data [$ I] [$ j] = $ row2 [$ j];
$ Data [$ j] = $ row2 [$ j];
$ Id3 = $ row2 [$ j] ['id'];
$ Sql3 = "select * from menutree where parentid = '$ id3 '";
$ Quer3 = $ this-> db-> query ($ sql3 );
$ Row3 = $ quer3-> result_array ();
If ($ quer3-> num_rows ()> 0 ){
$ This-> getArray ($ row3 );
}
}
}
}
}
Return $ data;
}

Foreach is replaced with

Mysql_fetch_array

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.