Yii中用遞歸方法實現無限級分類

用遞歸方法實現多級分類,適合分級不太多的分類,如三到四級。javascript

數據庫結構:php

Model中(Category.php)html

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

Controller中(CategoryController.php)java

    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擴展web

    <div class="main">
        <div class="category">
            <form action="/category/create" method="post">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th style="width:10px;"></th>
                        <th>分類名稱</th>
                        <th style="width: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樣式sql

 
/*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 數組結構如圖:數據庫

最終效果圖:數組

相關文章
相關標籤/搜索