PHP unlimited hierarchical data implementation program code _ PHP Tutorial

Source: Internet
Author: User
PHP unlimited hierarchical data implementation program code. Let's introduce some usage of unlimited classification in php, including operations on direct and unlimited classification databases and arrays. For more information, see. Let's take a look at some usage of unlimited classification in php, including operations on direct and unlimited classification databases and arrays. For more information, see.

Let's first look at an infinite classification of php and mysql databases.


Create a database:
Id, fid, and fname (both id and fid must be numerical and the default value of fid must be set to 0;

The code is as follows:

$ Stime = microtime ();

$ Db = @ mysql_connect ("localhost", "root", "micronsky.net") or die ("Database Connection Lost ");
Mysql_select_db ("temp", $ db );
// Define the first level of classification
Function mainfl ()
{
Global $ db;
$ Result = mysql_query ("select id, fid, fname from wxjfl where fid = 0 order by id desc", $ db );
If ($ myrow = mysql_fetch_array ($ result )){
Do {
Echo $ myrow ["fname"];
Echo"
";
Echo subfl ($ myrow ["id"], ""); // call the subcategory function here
}
While ($ myrow = mysql_fetch_array ($ result ));
}

}
// Define a subcategory
Function subfl ($ fid, $ strdis)
{
Global $ db;
$ Result1 = mysql_query ("select id, fid, fname from wxjfl where fid = $ fid order by id desc", $ db );
If ($ myrow1 = mysql_fetch_array ($ result1 )){
Do {
// Echo $ strdis;
Echo $ strdis. $ myrow1 ["fname"];
Echo"
";
Subfl ($ myrow1 ["id"], "". $ strdis); // you must note that the echo... you just need to directly call the subcategory function and pay the value! This is also the recursion part.
}
While ($ myrow1 = mysql_fetch_array ($ result1 ));
}
}


Echo mainfl ();

$ Ltime = microtime ();
Echo"
";
Echo number_format ($ ltime-$ stime, 4); // statistics execution time. This is much faster than ASP, but it has little to do with writing, this is mainly because PHP itself has joined the accelerator!

?>


Now we use arrays for paging.

The code is as follows:

/**
* Create a parent node tree array
* Parameters
* $ Ar array, data organized in the form of an adjacent list
* $ Subscripts or associated key names used as primary keys in the id array
* $ Subscript or associated key name of the parent key in the pid array
* Returns a multi-dimensional array.
**/
Function find_parent ($ ar, $ id = 'id', $ pid = 'pid '){
Foreach ($ ar as $ v) $ t [$ v [$ id] = $ v;
Foreach ($ t as $ k => $ item ){
If ($ item [$ pid]) {
If (! Isset ($ t [$ item [$ pid] ['parent'] [$ item [$ pid])
$ T [$ item [$ id] ['parent'] [$ item [$ pid] = & $ t [$ item [$ pid];

$ T [$ k] ['refer'] = true;
}
}
Return $ t;
}


/**
* Create a subnode tree array
* Parameters
* $ Ar array, data organized in the form of an adjacent list
* $ Subscripts or associated key names used as primary keys in the id array
* $ Subscript or associated key name of the parent key in the pid array
* Returns a multi-dimensional array.
**/
Function find_child ($ ar, $ id = 'id', $ pid = 'pid '){
Foreach ($ ar as $ v) $ t [$ v [$ id] = $ v;
Foreach ($ t as $ k => $ item ){
If ($ item [$ pid]) {
$ T [$ item [$ pid] ['child '] [$ item [$ id] = & $ t [$ k];

$ T [$ k] ['refer'] = true;
}
}
Return $ t;
}

Example:

The code is as follows:
$ Data = array (
Array ('id' => 1, 'parent' => 0, 'name' => 'grandpa '),
Array ('id' => 2, 'parent' => 1, 'name' => 'father '),
Array ('id' => 3, 'parent' => 1, 'name' => 'Uncle '),
Array ('id' => 4, 'parent' => 2, 'name' => 'your '),
Array ('id' => 5, 'parent' => 4, 'name' => 'son ')
);
$ P = find_parent ($ data, 'id', 'parent ');
$ C = find_child ($ data, 'id', 'parent ');

The above two methods are to spread all nodes to an array by id, find their parent or children, and link the elements to parent and children by referencing them,

However, the referenced elements still exist in an array. Therefore, in actual applications, it is best to mark those referenced elements to avoid traversal starting with them as the root, leading to repetition.

The code is as follows:


Foreach ($ p as $ key => $ item ){
If ($ item ['reference']) continue;
Print_r ($ item );
}

Foreach ($ c as $ key => $ item ){
If ($ item ['reference']) continue;
Print_r ($ item );
}

Recursive method: After the PHP array elements are deleted, the array cursor will return to zero. Therefore, some elements that have found a "destination" in the traversal process have to be left in the array, the search range of subsequent elements cannot be reduced:

The code is as follows:


$ Mylist = array ('parent _ id' => 0, 'id' => 1 ),
Array ('parent _ id' => 0, 'id' => 2 ),
Array ('parent _ id' => 0, 'id' => 3 ),
Array ('parent _ id' => 2, 'id' => 4 ),
Array ('parent _ id' => 2, 'id' => 5 ),
Array ('parent _ id' => 3, 'id' => 6 ),
Array ('parent _ id' => 3, 'id' => 7 ),
Array ('parent _ id' => 4, 'id' => 8 ),
Array ('parent _ id' => 5, 'id' => 9 ),
Array ('parent _ id' => 5, 'id' => 10)
);

Function _ findChildren ($ list, $ p_id) {// hierarchical data,
$ R = array ();
Foreach ($ list as $ id => $ item ){
If ($ item ['parent _ id'] = $ p_id ){
$ Length = count ($ r );
$ R [$ length] = $ item;
If ($ t = $ this-> _ findChildren ($ list, $ item ['id']) {
$ R [$ length] ['Children '] = $ t;
}
}
}
Return $ r;
}

Print_r (_ findChildren ($ mylist, 0 ));

Bytes. First look at...

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.