tp5递归 无限级分类详解
作者:琴酒丶 时间:2024-05-11 09:53:48
在TP5公共common.php文件里写
<?php
//计算某个类别所属的类别层数
function getcatelayer($cateid,$flag=1){
$cates = M('Gcategory');
$ini['cate_id'] = $cateid;
$arr = $cates->where($ini)->find();
if($arr['parent_id']!=0){
$flag = getcatelayer($arr['parent_id'],$flag+1);
}
return $flag;
}
//类别递归(单层)
function getone($arr,$parent=0,$lev=0){
//迭代
$task=array($parent);//任务数组
$tree=array();//结果数组
while (!empty($task)){
$flg=false;
foreach ($arr as $k=>$v){
if ($v['parent_id']==$parent) {
$tree[]=array(
'id'=>$v['cate_id'],
'name'=>$v['cate_name'],
'lev'=>$lev
);
array_push($task, $v['cate_id']);
$parent=$v['cate_id'];
$lev=$lev+1;
unset($arr[$k]);
$flg=true;
}
}
if ($flg==false) {
array_pop($task);
$parent=end($task);
$lev=$lev-1;
}
}
return $tree;
}
//类别递归(多层)
function getCates($arr,$pid=0)
{
for($i=0; $i<count($arr); $i++){
if($arr[$i]['parent_id']==$pid){
$newArr[] = array(
"id"=>$arr[$i]['cate_id'],
"name"=>$arr[$i]['cate_name'],
'son'=>getCates($arr,$arr[$i]['cate_id']),
);
}
}
return $newArr;
}
//查询类别所属级
function getlayer($cateid,$flg=1)
{
$gcg=M("Gcategory");
$ini["cate_id"]=$cateid;
$cateArr=$gcg->where($ini)->find();
if($cateArr['parent_id']!=0)
{
$flg=getlayer($cateArr['parent_id'],$flg+1);
}
return $flg;
}
一般只用其中的递归单层就可以了
控制器中
<?php
//分类管理
namespace app\admin\Controller;
use think\Db;
use think\Controller;
class CategoryController extends Controller
{
/**
* 分类列表页(递归)
*/
public function category_list()
{
header('content-type:text/html;charset=utf-8');
$categoryArr=db::name('表名')->select();
//调用递归函数
$getall=getone($categoryArr);
$this->assign("getall",$getall);
return $this->fetch()
}
视图 view中
<div class="conShow">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="100px" class="tdColor tdC">id</td>
<td width="150px" class="tdColor">汽车类型</td>
<td width="180px" class="tdColor">操作</td>
</tr>
{foreach name="getall" id="v"}
<tr>
<td>{$v.id}</td>
<td >{php}echo str_repeat(' ',$v['lev']*2){/php}|-{$v['name']}</td>
<td><a href="connoisseuradd.html" rel="external nofollow" ><img class="operation"
src="/img/admin/update.png"></a> <img class="operation delban"
src="/img/admin/delete.png"></td>
</tr>
{/foreach}
</table>
<div class="paging">此处是分页</div>
</div>
来源:https://blog.csdn.net/php12345679/article/details/79882337
标签:tp5,递归,无限级,分类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql、mssql及oracle分页查询方法详解
2024-01-21 15:11:34
在python中bool函数的取值方法
2021-10-06 00:47:37
bootstrap实现二级下拉菜单效果
2024-04-17 10:40:42
JS通过FSO将unicode字符写入文本
2009-06-01 12:26:00
MySQL数据库高级查询和多表查询
2024-01-14 06:55:46
![](https://img.aspxhome.com/file/2023/6/81176_0s.png)
在Python中通过threading模块定义和调用线程的方法
2022-03-08 23:23:49
解决mysql8.0.19 winx64版本的安装问题
2024-01-24 01:02:17
面试被问select......for update会锁表还是锁行
2024-01-17 20:59:17
![](https://img.aspxhome.com/file/2023/9/121579_0s.png)
python文件写入write()的操作
2023-11-10 04:33:11
利用python模拟实现POST请求提交图片的方法
2021-02-20 05:58:21
Python locust工具使用详解
2021-11-04 04:04:56
![](https://img.aspxhome.com/file/2023/3/83703_0s.png)
游戏中的交互设计
2008-08-25 19:20:00
![](https://img.aspxhome.com/file/UploadPic/20088/25/2008825192344309s.jpg)
Django中使用Celery的教程详解
2023-07-12 05:08:35
![](https://img.aspxhome.com/file/2023/7/91927_0s.png)
如何在python字符串中输入纯粹的{}
2023-09-21 22:56:41
Anaconda2下实现Python2.7和Python3.5的共存方法
2022-06-30 12:43:30
![](https://img.aspxhome.com/file/2023/8/97528_0s.jpg)
Python画图小案例之小雪人超详细源码注释
2021-09-21 11:49:44
SQL Server数据库入门学习总结
2024-01-21 18:23:25
![](https://img.aspxhome.com/file/2023/6/84346_0s.gif)
将多宿主计算机配置为SQL Server访问
2011-01-04 16:17:00
Python爬虫框架之Scrapy中Spider的用法
2023-10-04 10:38:21
浅谈Django 页面缓存的cache_key是如何生成的
2022-03-20 09:53:02