php无限极分类php无限极分类是经常要用到的,本人以前一直用的是已经写好的,所以没仔细去研究过,今天由于项目的需要,需要临时做一个php无限极分类的东东,遂做了一个最简单的,现记录如下,有需要的朋友可以看下。
数据表结构
create table if not exists `category` ( `id` int(5) not null auto_increment comment '唯一自增id', `pid` int(5) not null default '0' comment '父id', `sort` int(2) not null default '0' comment '排序数字', `name` varchar(30) default null comment '名称', primary key (`id`)) engine=myisam default charset=utf8 comment='无限极分类表' auto_increment=1 ;
数据
insert into `category` (`id`, `pid`, `sort`, `name`) values(1, 0, 1, 'php'),(2, 0, 2, '数据库'),(3, 0, 3, 'javascript'),(4, 1, 1, '框架模板'),(5, 1, 2, '函数总结'),(6, 2, 1, 'mysql'),(7, 4, 1, '框架'),(8, 4, 2, '模板'),(9, 8, 1, 'smarty'),(10, 7, 2, 'thinkphp'),(11, 10, 1, 'thinkphp技巧'),(12, 10, 2, 'thinkphp模板'),(13, 12, 3, '模板知识总结'),(14, 12, 2, '模板视频教程'),(15, 11, 1, 'model技巧');
函数实现代码
function tree(&$list,$pid=0,$level=0,$html='--'){ static $tree=array(); foreach($list as $v){ if($v['pid']==$pid){ $v['level']=$level; $v['html']=str_repeat($html,$level); $tree[]=$v; tree($list,$v['id'],$level+1,$html); } } return $tree;}
以上tree函数的第一个参数$list就是获取的如上表的一个二维数组的结果集。需要注意的是从数据库获取结果集的sql语句必须加上order by sort asc,否则sort字段将不能发挥排序的作用。
您可能感兴趣的文章php清空(删除)指定目录下的文件,不删除目录文件夹的方法php获取某段时间内每个月的方法,返回由这些月份组成的数组php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法php setcookie无效的原因分析php 获取今天明天昨天时间戳的 n 种方法总结php压缩html网页代码(清除空格,换行符,制表符,注释标记)php新手用的insert和update语句构造类php中如何将数组变量写入到文件
http://www.bkjia.com/phpjc/1058849.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1058849.htmltecharticlephp无限极分类 php无限极分类是经常要用到的,本人以前一直用的是已经写好的,所以没仔细去研究过,今天由于项目的需要,需要临时做一...