Home > Backend Development > PHP Tutorial > thinkPHP implements a method of recursively looping columns and outputting infinitely according to the tree structure

thinkPHP implements a method of recursively looping columns and outputting infinitely according to the tree structure

不言
Release: 2023-03-25 12:42:02
Original
2901 people have browsed it

This article mainly introduces the method of thinkPHP to implement recursive loop columns and output infinite pole according to the tree structure, involving thinkPHP database query, array traversal and string operation and other skills. Friends who need it can refer to it

The example of this article describes the method of thinkPHP to implement recursive loop columns and output infinitely according to the tree structure. Share it with everyone for your reference, the details are as follows:

Here we use thinkphp recursive loop column to output infinitely according to the tree structure, and save it as an array, which is convenient for template calling

The specific code is as follows:

private function categoryTree($parentid,$level)
//因为是本类中使用所以定于为私有函数
{
$Category= D('Category');
$result = $Category->where("`parentid`=".$parentid)->order("listorder desc,catid desc")->select();
if($result)
{
$count=count($result);//当前子栏目个数
$level++;//子栏目层级
foreach($result as $v)
{
$index++;
if($count==$index) $step="└─";
else $step="├─";
$step.=str_repeat('  ',$level-1);
$nbsp=str_repeat('   ',$level-1);
$nstr=$nbsp.$step;
if($parentid==0) $nstr='';
$v['step']=$nstr;
$newData[$v['catid']]=$v;
//echo $nstr.$v[&#39;catname&#39;]."<br />";
if($v[&#39;child&#39;]==1)//如果有子栏目
{
$newData=$newData+$this->categoryTree($v[&#39;catid&#39;],$level);
}
}
}
return $newData;
}
Copy after login

php recursive columns are saved as arrays

Related recommendations:

thinkPHP implements multi-field fuzzy matching query Method

thinkphp implements file upload and file download

The above is the detailed content of thinkPHP implements a method of recursively looping columns and outputting infinitely according to the tree structure. 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