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="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} {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> <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} {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> <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} {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> <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 数组结构如图:
最终效果图: