Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Realization method of Infinite Pole Classification in mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the implementation of infinite pole classification in mysql, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I also found it from somewhere else. After analysis, I found it was very good, so I came to share it.

If you cannot send a picture, you can only simply deal with it:

Id name pid path

1 News 0 0

China News 1 0-1

3 US News 1 0-1

Beijing News 20-1-2

5 fingernail news 30-1-3

One sentence is done:

SELECT id,name,pid,path,concat (path,'-',id) AS newpath FROM cates ORDER BY newpath

The result after query:

Id name pid path newpath

1 News 0 0 0 1

2 China News 10-10-1-2

Beijing News 20-1-20-1-2-4

3 US News 10-10-1-3

5 fingernail news 30-1-30-1-3-5

It can be seen that the structure is already very clear. Very skillfully use the sorting function of mysql, use the concat () function to connect the current path with the current id (want to connect the string very much), give an individual name newpath, in sorting newpath, coincidentally, compare 0 first, compare 1, compare 2, compare 3. In this way, the query results are sorted directly according to the previous level, and then you only need to cycle through the output in the foreground, and the "hierarchical placeholder" can also be solved. The 0-1-2-4 of newpath is equivalent to an infinite pole placeholder. When php is output, you need to use the above statement to query it, and process the newpath field value of each piece of data in the background. First, divide the value into an array, and count the array. Then add a key count (a set of data) to the array, and put the number of statistical arrays in this key.

Thinkphp 5.0as an example:

$list = CateModel::fild ('id,name,pid,path,concat (path,'-',id)')-> order ('newpath asc')-> select (); / / the above model query method is only a general meaning and can be spelled out in tp5 way according to the top mysql statement. Foreach ($list as $k = > v) {$list [$k] ['count'] = count (explode (' -', $v))}

This count is the placeholder for the hierarchical classification, and then loop it in the template, echo a few times.

{volist name= "list" id= "vo"} {php} for

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report