• Yii中用递归方法实现无限级分类


    用递归方法实现多级分类,适合分级不太多的分类,如三到四级。

    数据库结构:

    Model中(Category.php)

        /**
         * 获取全部分类信息
         */
        public function getAllcategory()
        {        
            $sql = 'select * from '.$this->tableName().' order by id asc';
            $category = ZDBTool::QueryAll($sql);
            
            return $category;
        } 

    Controller中(CategoryController.php)

        public function actionIndex()
        { 
            global $category;
            $category = array();
    
            $category = Category::model()->getAllCategory();
            //print_r($category);
    
            //……其它内容省略
    
            $param = array(
                'model' => $model,
                'cat_arr' => $cat_arr,
                );
            $this->render('index', $param);
        }
    
        //无限分类递归数组
        public function get_cat_array($pid = 0)
        {
            //echo 'fid:'.$fid.' ';
            global $category;
            $arr = array();
            foreach($category as $index => $row){
                //对每个分类进行循环。 
                if($category[$index]['pid'] == $pid){ //如果有子类        
                    $row['child'] = $this->get_cat_array($category[$index]['id']); //调用函数,传入参数,继续查询下级
                    $arr[] = $row; //组合数组  
                }  
            }
            return $arr;
        }

    View中(category/index.tpl)(本文只演示到三级分类,此处使用了Yii的smarty-view-renderer扩展

        <div class="main">
            <div class="category">
                <form action="/category/create" method="post">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th style="10px;"></th>
                            <th>分类名称</th>
                            <th style="80px;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td colspan="4" class="border_btm" style="padding-top:15px;">
                                <a href="{$this->createUrl('category/create')}"><i class="icon-plus-sign-alt">+</i> 添加新分类</a>
                            </td>
                        </tr>
                    {foreach from=$cat_arr key=k item=v}
                        <tr>
                            <td>{if !empty($v.child)}<a href="javascript:;"><i class="icon-chevron-down"></i></a>{/if}</td>
                            <td>
                                <div class="type-parent">{$v.title}&nbsp;&nbsp;
                                {if empty($row['pid'])}<a href="{$this->createUrl('category/create', ['pid'=>$v.id])}"><i class="icon-plus-sign-alt">+</i> 添加子分类</a>{/if}
                                </div>
                            </td>
                            <td>
                                <a href="{$this->createUrl('category/update', ['id'=>$v.id])}">编辑</a>&nbsp;&nbsp;
                                <a href="{$this->createUrl('category/delete', ['id'=>$v.id])}" 
                                onclick="return confirm('确认删除此分类吗?');return false;">删除</a>
                            </td>
                        </tr>
                        {foreach from=$v.child key=k1 item=v1}
                        <tr>
                            <td>{if !empty($v1.child)}<a href="javascript:;"><i class="icon-chevron-down"></i></a>{/if}</td>
                            <td>
                                <div class="type-child">{$v1.title}&nbsp;&nbsp;{if $v1.pid!=0}
                                <a href="{$this->createUrl('category/create', ['pid'=>$v1.id])}">
                                <i class="icon-plus-sign-alt">+</i> 添加子分类</a>{/if}</div>
                            </td>
                            <td>
                                <a href="{$this->createUrl('category/update', ['id'=>$v1.id])}">编辑</a>&nbsp;&nbsp;
                                <a href="{$this->createUrl('category/delete', ['id'=>$v1.id])}" 
                                onclick="return confirm('确认删除此分类吗?');return false;">删除</a>
                            </td>
                        </tr>
                            {foreach from=$v1.child key=k2 item=v2}
                            <tr>
                                <td></td>
                                <td>
                                    <div class="type-child-child">{$v2.title}&nbsp;&nbsp;{if $v2.pid!=0}
                                    <a href="{$this->createUrl('category/create', ['pid'=>$v2.id])}">
                                    <i class="icon-plus-sign-alt">+</i> 添加子分类</a>{/if}</div>
                                </td>
                                <td>
                                    <a href="{$this->createUrl('category/update', ['id'=>$v2.id])}">编辑</a>&nbsp;&nbsp;
                                    <a href="{$this->createUrl('category/delete', ['id'=>$v2.id])}" 
                                    onclick="return confirm('确认删除此分类吗?');return false;">删除</a>
                                </td>
                            </tr>
                            {/foreach}
                        {/foreach}
                    {/foreach}
                        <tr>
                            <td></td>
                            <td colspan="4">
                                <input name="submit" type="submit" class="btn button green" value="提交">
                            </td>
                        </tr>
                    </tbody>
                </table>
                </form>
            </div>
        </div>

    CSS样式

    
    
    /*Category*/
    .category{padding:15px;}
    .category .table td{/*font-size:16px;*/ vertical-align:middle;}
    .category .table td input{margin-bottom:0;}
    .category .table .type-child{padding-left:55px;background:url('../../images/bg_repno.gif') no-repeat -248px -550px;}
    .category .table .type-child-child{padding-left:105px;background:url('../../images/bg_repno.gif') no-repeat -248px -550px;}
    
    
    附:bg_repno.gif

    $cat_arr 数组结构如图:

    最终效果图:

  • 相关阅读:
    c# .net 均适用根据经纬度坐标获得省市区县行政区划城市名称
    .NET LoongArch64 正式合并进入.NET
    Microk8s 安装 与使用指南
    Dapr 不是服务网格,只是我长的和他很像
    从同步函数 helloworlddotnet 开始探索OpenFunction
    CoreWCF 1.0.0 发布,微软正式支持WCF
    Microsoft Graph 的 .NET 6 之旅
    .NET MAUI RC2 发布,支持 Tizen 平台
    [LeetCode] 1296. Divide Array in Sets of K Consecutive Numbers 划分数组为连续数字的集合
    [LeetCode] 1297. Maximum Number of Occurrences of a Substring 子串的最大出现次数
  • 原文地址:https://www.cnblogs.com/imxiu/p/3463873.html
Copyright © 2020-2023  润新知