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:;}
.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 数组结构如图:

最终效果图:

Yii中用递归方法实现无限级分类的更多相关文章
- (实用篇)PHP递归实现无限级分类
在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类. 在一些复杂的系统中,要求对信 ...
- PHP无限级分类-递归(不推荐)
[http://www.helloweba.com/view-blog-204.html] 在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢? ...
- php无限级分类
使用递归方法,遍历子类,对数据进行重新排序,使用level进行无限级分类 /** * 功能:无限级分类 * 参数:$data 类别查询结果集 * 返回值:$arr 排序后的数组 */ public f ...
- PHP递归实现无限级分类
在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类. 在一些复杂的系统中,要求对信 ...
- FreeSql 使用 ToTreeList/AsTreeCte 查询无限级分类表
关于无限级分类 第一种方案: 使用递归算法,也是使用频率最多的,大部分开源程序也是这么处理,不过一般都只用到四级分类. 这种算法的数据库结构设计最为简单.category表中一个字段id,一个字段fi ...
- mysql无限级分类
第一种方案: 使用递归算法,也是使用频率最多的,大部分开源程序也是这么处理,不过一般都只用到四级分类. 这种算法的数据库结构设计最为简单.category表中一个字段id,一个字段fid(父id).这 ...
- PHP无限级分类的实现(不使用递归)
无限级分类在开发中经常使用,例如:部门结构.文章分类.无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式: 查找分类A下面所有分类包含的文章. 1.实现原理 在 ...
- 一道无限级分类题的 PHP 实现
今天有网友出了道题: 给出如下的父子结构(你可以用你所用语言的类似结构来描述,第一列是父,第二列是子),将其梳理成类似如图的层次父子结构. origin = [('A112', 'A1122'), ( ...
- php利用递归函数实现无限级分类
相信很多学php的很多小伙伴都会尝试做一个网上商城作为提升自己技术的一种途径.各种对商品分类,商品名之类的操作应该是得心应手,那么就可以尝试下无限级分类列表的制作了. 什么是无限级分类? 无限级分类是 ...
随机推荐
- 面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
package com.Amazon.interview; /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.c ...
- Python 命令行参数解析
方法1: Python有一个类可以专门处理命令行参数,先看代码: #!/usr/bin/env python # encoding: utf-8 from optparse import Option ...
- 转 Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再废叙述废话. 序言 对于所谓的认证说到 ...
- 基础canvas应用-钟表绘制
首先,canvas语法基础薄弱的小伙伴请点这里,剩下的小伙伴们可以接着往下看了. 一个表,需要画什么出来呢:3条线(时分秒针),1个圆(表盘),以及60条短线/点(刻度). 嗯,没毛病. 那接下来让我 ...
- App 性能分析
关键因素: ---- Instrument 性能分析神器 1. 启动时间 应用启动时间长短对用户第一次体验至关重要,同时系统对应用的启动.恢复等状态的运行时间也有严格的要求,在应用超时的情况下系统会直 ...
- Ajax、Comet、HTML 5 Web Sockets技术比较分析
最近因为考虑研究B/S结构网站即时消息处理 参考了 JAVA怎么样实现即时消息提醒http://bbs.csdn.net/topics/330015611http://www.ibm.com/deve ...
- asp.net viewstate的模拟登陆
其实 VIEWSTATE 不用太在意,倒是 JTCookieID 需要注意,这个才应该是服务器上用来维护 Session 的那个 Cookie.所以,你用 httpclient 的时候,不能上来就直接 ...
- bzoj 3784: 树上的路径 堆维护第k大
3784: 树上的路径 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 88 Solved: 27[Submit][Status][Discuss] ...
- [原博客] HEOI2014 行记
HEOI: 河北省信息学竞赛省队选拔赛 HEOI数据标程下载 百度盘 http://pan.baidu.com/s/1qWx7YAo 又到了一年一度的HEOI呢. 我果然还是太弱了呢. Day0 报到 ...
- matlab中 hold on 与hold off的用法
matlab中 hold on 与hold off的用法 hold on 是当前轴及图形保持而不被刷新,准备接受此后将绘制 hold off 使当前轴及图形不在具备被刷新的性质 hold on 和ho ...