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 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: |
Copy code |
<? Php $ 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 "<br> "; 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 "<br> "; 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 "<br> "; 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: |
Copy code |
/** * 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: |
Copy code |
$ 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: |
Copy code |
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: |
Copy code |
$ 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 )); |