Output the document category
List form;
- Search for articles contained in all categories under Category.
1. implementation principle
Several common implementation methods have their own advantages and disadvantages. The data structure of "improved forward traversal tree" is easy to output and query, but it is complicated in terms of mobile classification and general understanding.
2. Data structure
<? Php $ list = array ('id' => 1, 'fid' => 0, 'title' => 'China '), array ('id' => 2, 'fid' => 1, 'title' => 'Jiangsu '), array ('id' => 3, 'fid' => 1, 'title' => 'Anhui '), array ('id' => 4, 'fid' => 8, 'title' => 'jiangyin '), array ('id' => 5, 'fid' => 3, 'title' => 'wuhu '), array ('id' => 6, 'fid' => 3, 'title' => 'Hefei '), array ('id' => 7, 'fid' => 3, 'title' => 'bengbu '), array ('id' => 8, 'fid' => 8, 'title' => 'Wuxi ');?>
Each category is connected by its parent class id (fid) to form a classification tree. It is worth noting that the fid of classification A cannot be the id of its subclass.
When using this data structure for output, the most common algorithm is "recursion". friends familiar with the PHP language certainly know that PHP is not good at recursion and has a limited number of recursion times (about 100 times, operating system and configuration vary ).
Since all recursion can be implemented cyclically, this article has compiled a set of functions for "infinite" classification based on the characteristics of PHP language, which is more efficient than recursive implementation.
3. output ul List format
Output the above data as the following HTML
This HTML structure is very convenient for front-end use (using JavaScript and CSS to construct a foldable tree. The specific implementation procedure is as follows:
<?php echo get_tree_ul($list, 1); ?>
4. output option List format
Jiangsu Wuxi Jiangyin Anhui province Wuhu Hefei Bengbu
The specific implementation procedure is as follows:
<? Php // get_tree_option () returns an array and adds a "depth" (depth) column to each element. you can directly output $ options = get_tree_option ($ list, 1 ); foreach ($ options as $ op) {echo''. Str_repeat ("", $ op ['demo'] * 4). $ op ['title'].' <;/option> ';}?> <;/Select>5. search for all subclasses of a category<? Php $ children = get_tree_child ($ list, 0); echo implode (',', $ children); // output:,?>6. search for all parent classes of a category<? Php $ children = get_tree_parent ($ list, 4); echo implode (',', $ children); // 8, 2, 10?>7. related functions<? Phpfunction get_tree_child ($ data, $ fid) {$ result = array (); $ fids = array ($ fid); do {$ cids = array (); $ flag = false; foreach ($ fids as $ fid) {for ($ I = count ($ data)-1; $ I >=0; $ I --) {$ node = $ data [$ I]; if ($ node ['fid'] ==$ fid) {array_splice ($ data, $ I, 1 ); $ result [] = $ node ['id']; $ cids [] = $ node ['id']; $ flag = true ;}}} $ fids = $ cids;} while ($ flag = true); return $ result;} function get_t Ree_parent ($ data, $ id) {$ result = array (); $ obj = array (); foreach ($ data as $ node) {$ obj [$ node ['id'] = $ node;} $ value = isset ($ obj [$ id])? $ Obj [$ id]: null; while ($ value) {$ id = null; foreach ($ data as $ node) {if ($ node ['id'] = $ value ['fid']) {$ id = $ node ['id']; $ result [] = $ node ['id']; break ;}} if ($ id = null) {$ result [] = $ value ['fid'];} $ value = isset ($ obj [$ id])? $ Obj [$ id]: null;} unset ($ obj); return $ result;} function get_tree_ul ($ data, $ fid) {$ stack = array ($ fid ); $ child = array (); $ added_left = array (); $ added_right = array (); $ html_left = array (); $ html_right = array (); $ obj = array (); $ loop = 0; foreach ($ data as $ node) {$ pid = $ node ['fid']; if (! Isset ($ child [$ pid]) {$ child [$ pid] = array ();} array_push ($ child [$ pid], $ node ['id']); $ obj [$ node ['id'] = $ node;} while (count ($ stack)> 0) {$ id = $ stack [0]; $ flag = false; $ node = isset ($ obj [$ id])? $ Obj [$ id]: null; if (isset ($ child [$ id]) {$ cids = $ child [$ id]; $ length = count ($ cids); for ($ I = $ length-1; $ I >=0; $ I --) {array_unshift ($ stack, $ cids [$ I]);} $ obj [$ cids [$ length-1] ['islastchild '] = true; $ obj [$ cids [0] ['isfirstchild '] = true; $ flag = true;} if ($ id! = $ Fid & $ node &&! Isset ($ added_left [$ id]) {if (isset ($ node ['isfirstchild ']) & isset ($ node ['islastchild']) {$ html_left [] ='';} Else if (isset ($ node ['isfirstchild']) {$ html_left [] ='';} Else if (isset ($ node ['islastchild']) {$ html_left [] ='';} Else {$ html_left [] ='';} $ Html_left [] = ($ flag = true )? "{$ Node ['title']}":"{$ Node ['title']}"; $ Added_left [$ id] = true;} if ($ id! = $ Fid & $ node &&! Isset ($ added_right [$ id]) {$ html_right [] = ($ flag = true )? '':''; $ Added_right [$ id] = true;} if ($ flag = false) {if ($ node) {$ cids = $ child [$ node ['fid']; for ($ I = count ($ cids)-1; $ I >=0; $ I --) {if ($ cids [$ I] ==$ id) {array_splice ($ child [$ node ['fid'], $ I, 1); break ;}} if (count ($ child [$ node ['fid']) = 0) {$ child [$ node ['fid'] = null ;}} array_push ($ html_left, array_pop ($ html_right); array_shift ($ stack) ;}$ loop ++; if ($ loop> 5000) return $ html_l Eft;} unset ($ child); unset ($ obj); return implode ('', $ html_left);} function get_tree_option ($ data, $ fid) {$ stack = array ($ fid); $ child = array (); $ added = array (); $ options = array (); $ obj = array (); $ loop = 0; $ depth =-1; foreach ($ data as $ node) {$ pid = $ node ['fid']; if (! Isset ($ child [$ pid]) {$ child [$ pid] = array ();} array_push ($ child [$ pid], $ node ['id']); $ obj [$ node ['id'] = $ node;} while (count ($ stack)> 0) {$ id = $ stack [0]; $ flag = false; $ node = isset ($ obj [$ id])? $ Obj [$ id]: null; if (isset ($ child [$ id]) {for ($ I = count ($ child [$ id])-1; $ I> = 0; $ I --) {array_unshift ($ stack, $ child [$ id] [$ I]);} $ flag = true;} if ($ id! = $ Fid & $ node &&! Isset ($ added [$ id]) {$ node ['dest'] = $ depth; $ options [] = $ node; $ added [$ id] = true ;} if ($ flag = true) {$ depth ++;} else {if ($ node) {for ($ I = count ($ child [$ node ['fid'])-1; $ I >=0; $ I --) {if ($ child [$ node ['fid'] [$ I] ==$ id) {array_splice ($ child [$ node ['fid'], $ I, 1); break; }}if (count ($ child [$ node ['fid']) = 0) {$ child [$ node ['fid'] = null; $ depth -- ;}} array_shift ($ stack) ;}$ loo P ++; if ($ loop> 5000) return $ options;} unset ($ child); unset ($ obj); return $ options ;}?>The above section describes how to use php to implement Infinitus classification without recursion. I hope this will be helpful for your learning.Recursive (no recursion is used). php recursive unlimited classification is often used in development, such as department structure and document classification. The difficulties of an unlimited classification are "output" and...