Home > Database > Mysql Tutorial > body text

Code implementation of infinite classification in mysql

不言
Release: 2018-08-20 16:34:00
Original
2736 people have browsed it

The content of this article is about the code implementation of Infinitus classification in mysql. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I also found it from other places. After analyzing it, I found it to be quite good, so I would like to share it here.

Can't post a picture, but it can only be handled simply:

ID name Pid PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID PID

3 American News 1 0-1

4 Beijing News 2 0-1-2

## 5 Nail Brothers News 3 0-1-3

## :

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

Result after query:

id                                                                                                                                                                                                                                                                               ​news 1                                                                                                                                                             Since 0-1- 3 0-1-3

5                                                                                                                                                                                                                                .

##It can be seen that the structure is very clear. It very cleverly uses the sorting function of mysql. It uses the concat() function to connect the current path with the current id (I really want to connect strings). It gives an alias newpath and sorts newpath. Coincidentally, it compares 0 first when sorting. , Comparing 1, Comparing 2, Comparing 3... In this way, the query results are sorted directly by level. Next, you only need to loop output in the foreground, and the "hierarchical relationship placeholder" can also be solved. 0-1-2-4 of newpath is equivalent to the Infinitus placeholder. When outputting PHP, you need to use the above statement to query it. The newpath field value of each piece of data is processed in the background. The value is first divided into arrays, and then in statistics For this array, add a key count (a set of data) to the array, and put the counted number of arrays into this key.

Thinkphp 5.0 as an example:

$list = CateModel::fild(‘id,name,pid,path,concat(path,’-‘,id)’)->order(‘newpath asc’)->select();
// 上面模型查询方法只是大概意思,可以根据最上面的 mysql 语句,用 tp5 的方式拼出来。
foreach ($list as $k=>v){
$list[$k][‘count’] = count(explode(‘-‘,$v))
}
Copy after login

This count is the placeholder for hierarchical classification, then loop it in the template, echo several times after looping it

<select name=”aa”>
{volist name=”list” id=”vo”}
<option vlaue=”{$vo.id}”>
{php}
for($i=0;$i<$vo[‘count’]*2;$i++){
echo ‘ ’
}
{/php}
{$vo.name}
</option>
{/volist}
</select>
Copy after login

Effect:

News

China News

                                                                                                                                                          ’ ’ s ’ s ’ s ’ s ’ s ’s ’ ’ s ​ ’ ​ to

#How to use recursion to achieve infinite grading under the TP5 framework (code example)

The above is the detailed content of Code implementation of infinite classification in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!