首页 > 其他 > 详细

无限极分类获取数据方法

时间:2020-01-03 20:09:45      阅读:65      评论:0      收藏:0      [点我收藏+]
<?php
namespace app\admin\model;

use think\Model;

class GoodsCate extends Model
{

//获取分类树 public function get_tree ($pid = 0,$field = ‘*‘){ $list = self::all(function($query)use($pid,$field){ $query->where([‘pid‘=>$pid])->field($field); }); foreach ($list as &$v){ if(self::get([‘pid‘=>$v[‘id‘]])){ $v[‘child‘] = self::get_tree($v[‘id‘],$field); } } return $list; } //根据id获取所有层级父类id public static function get_parent ($id = 0){ $list = self::all(); $arr = array_column($list,‘pid‘,‘id‘); $ids[] = $id; while($arr[$id]) { $id = $arr[$id]; $ids[] = $id; } return $ids; } //根据id获取所有子级id public static function get_child ($id,&$ids=[]){ array_push($ids,$id); $list = self::where(‘pid‘,$id)->column(‘id‘); foreach ($list as $v) { self::get_child($v,$ids); } return $ids; } //根据id获取当前数据及其子类列表 public function get_list ($id=0,$field=‘*‘,$where){ $info = self::where(‘id‘,$id)->alias(‘goods_cate‘)->field($field)->find(); if($info) { $list[] = $info; } $list = $this->get_data($id,$list,$field,$where); return $list; } public function get_data($id,&$list=[],$field,$where) { $child = self::where(‘pid‘,‘in‘,$id)->alias(‘goods_cate‘)->field($field)->where($where)->select(); if($child) { foreach ($child as $item) { $list[] = $item; self::get_data($item[‘id‘], $list,$field,$where); } } return $list; } }

无限极分类获取数据方法

原文:https://www.cnblogs.com/ivy-zheng/p/12146254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!