In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to create infinite pole classification tree structure in php". In daily operation, I believe many people have doubts about how to create infinite pole classification tree structure in php. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to create infinite pole classification tree structure in php". Next, please follow the editor to study!
Put up the effect picture first
In fact, the top-level classification is the first-level classification, and the second-level classification is also called the sub-classification of the first-level classification. on this basis, the sub-classification can also have a sub-classification, which constitutes the infinite polar classification.
Next, let's take a look at the specific implementation code:
First, query by field in the controller to find out all the classification information (id: ID value of the category, cate_name: the name of the category, pid: parent ID,sorts: prepare to display the order of the title, but do not write. )
Public function cate_display () {$cate = D ('Cate'); $field = array (' id','cate_name','pid','sorts'); $list = $cate- > allCategory ($field); $this- > assign ('list',$list); $this- > display ();}
Second, the code in the model
Create two methods in the model of the corresponding controller
1. Query all classification information and call the method of generating the classification tree:
Public function allCategory ($field='*') {$data = $this- > field ($field)-> select (); return $this- > tree ($data);}
two。 Generate a classification tree (using recursion, passing in data, and pid [parent id], level [number of layers, used to control the number of display] two variables with an initial value of zero)
Public function tree ($data,$pid=0,$level=0) {static $tree = array (); foreach ($data as $k = > $v) {if ($v ['pid'] = = $pid) {$v [' level'] = $level; $tree [] = $v; $this- > tree ($data,$v ['id'], $level+1);} return $tree;}
Code in the view file
Superior menu top-level menu {$val.cate_name}
In this way, a classification tree structure that can be infinitely recursive is completed. Summary: the core idea is still a recursive function in the model, which is passed into pid by default zero, and then each recursive input id,level is used by the superior to record the number of recursive layers. Finally, when the view page is displayed, the built-in function str_repeat () of PHP is called to repeatedly output'-'to achieve the effect of distinguishing the series when output.
At this point, the study on "how to create an infinite pole classification tree structure by php" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.