• 无限级分类


    yii2 递归无限级分类
    
          模型层
    <?php
    
    namespace frontendmodels;
    
    use Yii;
    
    /**
     * This is the model class for table "region".
     *
     * @property integer $region_id
     * @property integer $parent_id
     * @property string $region_name
     * @property integer $region_type
     */
    class Region extends yiidbActiveRecord
    {
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'region';
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['parent_id', 'region_type'], 'integer'],
                [['region_name'], 'string', 'max' => 120]
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'region_id' => 'Region ID',
                'parent_id' => 'Parent ID',
                'region_name' => 'Region Name',
                'region_type' => 'Region Type',
            ];
        }
         /**
         * 地区分类
         */
        public function getinfo()
        {
            $connection = Yii::$app->db;
            $region=$connection->createCommand('SELECT * FROM region where region_type != 0')->queryAll();
            return $this->left($region,$parent_id=1);
        }
        public function left($region,$parent_id)
        {
            $child=array();
            foreach($region as $key=>$v)
            {
                if($v['parent_id']==$parent_id)
                {
                    $child[]=$v;
                }   
            }
            if(empty($child))
            {
                return null;
            }
            foreach($child as $key=>$s)
            {
                $kk_id=$this->left($region,$s['region_id']);
                if($kk_id){
                    $child[$key]['child']=$kk_id;
                }
            }
            return $child;
        }
    }
    
    
          控制器
    
    	$region = new Region();
    	$ros = $region->getinfo();
    	//print_r($ros);die;
    	return $this->renderPartial('companylist',['region'=>$ros]);
    
          视图层
    
    <? foreach($region as $key=>$reg) { ?>
    	<dl>
    		<dt><?= $reg['region_name'] ?></dt>
    		<dd>
    			<? foreach($reg['child'] as $k=>$r) { ?>
    				<span><?= $r['region_name'] ?></span>
    			<? } ?>
    		</dd>
    	</dl>
    <? } ?>
    
        *************递归**************
    public function getRows(){
            $data = $this->find()->asArray()->All();
            return $this->nolimitRows($data,$parent_id=0);
        }
        public function nolimitRows($data,$parent_id){
            $child=array();
            foreach ($data as $key => $val) {
                if($val['parent_id'] == $parent_id){
                    $child[] = $val;
                }
            }
            if(empty($child)){
                return null;
            }
            foreach ($child as $key => $v) {
                $arr = $this->nolimitRows($data,$v['type_id']);
                if($arr){
                    $child[$key]['child'] = $arr;
                }
            }        
            return $child;
        }

        *************无限级分类************
    public function getInfo(){
            $sql="select * from type";
            $data=$this->findBySql($sql)->asArray()->all();
            return $this->noLimitType($data,$parent_id=0,$level=0);
        }   
        public function noLimitType($data,$parent_id=0,$level=0){
            static $lists=array();
            foreach($data as $key=>$v){
                if($v['parent_id']==$parent_id){
                    $v['level']=$level;
                    $lists[]=$v;
                    $this->noLimitType($data,$v['type_id'],$level+1);
                }
            }
            return $lists;
        }
  • 相关阅读:
    rpm 命令详解
    自动配置原理
    ssm框架整合
    单个库创建用户和权限
    Mysql5.7安装过程
    Eclipse和JDK版本以及位数对应关系
    DHCP服务器
    常用Dos命令
    八、Linux上常用网络操作
    数据库分区表(转)
  • 原文地址:https://www.cnblogs.com/jhy-ocean/p/5364870.html
Copyright © 2020-2023  润新知