码迷,mamicode.com
首页 > 其他好文 > 详细

无限极分类获取数据方法

时间:2020-01-03 19:36:14      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:php   for   names   div   space   array   select   ace   sele   

<?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; } }

无限极分类获取数据方法

标签:php   for   names   div   space   array   select   ace   sele   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!