PHP递归实现层级树状展现数据 1. [代码] [PHP]代码 ?php $db = mysql_connect('localhost', 'root', 'root') or die('Can\'t connect to database'); mysql_select_db('test') or die('Can\'t find database : test'); $result = mysql_query
1. [代码][PHP]代码
<?php $db = mysql_connect('localhost', 'root', 'root') or die('Can\'t connect to database'); mysql_select_db('test') or die('Can\'t find database : test'); $result = mysql_query('select id, fid, name from tree'); while($arr = mysql_fetch_array($result)){ $data[] = array( 'id' => $arr['id'], 'fid' => $arr['fid'], 'name' => $arr['name'], ); } // 将数据按照缩进简单排列 见图1 function data2arr($tree, $rootId = 0, $level = 0) { foreach($tree as $leaf) { if($leaf['fid'] == $rootId) { echo str_repeat(' ', $level) . $leaf['id'] . ' ' . $leaf['name'] . '<br/>'; foreach($tree as $l) { if($l['fid'] == $leaf['id']) { data2arr($tree, $leaf['id'], $level + 1); break; } } } } } data2arr($data); echo '<br/>-----------------------------------------------------------------------<br/>'; // 将数据按照所属关系封装 见图2 function arr2tree($tree, $rootId = 0) { $return = array(); foreach($tree as $leaf) { if($leaf['fid'] == $rootId) { foreach($tree as $subleaf) { if($subleaf['fid'] == $leaf['id']) { $leaf['children'] = arr2tree($tree, $leaf['id']); break; } } $return[] = $leaf; } } return $return; } $tree = arr2tree($data); print_r($tree); echo '<br/>-----------------------------------------------------------------------<br/>'; // 将数据使用HTML再次展现 见图3 function tree2html($tree) { echo '<ul>'; foreach($tree as $leaf) { echo '<li>' .$leaf['name']; if(! emptyempty($leaf['children'])) tree2html($leaf['children']); echo '</li>'; } echo '</ul>'; } tree2html($tree);
2. [图片] 11.png
3. [图片] 22.png
4. [图片] 33.png
5. [代码][Shell/批处理]代码
当你学习php无限极分类的时候,大家都觉得一个字“难”我也觉得很难,所以,现在都还在看,因为工作要用到,所以,就必须得研究研究。 到网上一搜php无限极分类,很多,但好多都是一个,并且,写的很乱,代码很多,让我们怎么学习嘛,那些都不靠谱,还是自己捣鼓捣鼓无限极分类了。 比如一个category表:有id, name, pid, sort 就这四个简单的字段,不要太复杂了。 id name pid sort 1 PHP 0 1 2 Javascript 0 2 3 MySQL 0 3 4 PHP类 1 1 5 smarty 1 2 6 私有方法 4 1 7 jQuery 2 1 代码:category.class.php class Category { static public function sortOut($cate,$pid=0,$level=0,$html='--'){ $tree = array(); foreach($cate as $v){ if($v['id'] == $pid){ $v['level'] = $level + 1; $v['html'] = str_repeat($html, $level); $tree[] = $v; $tree = array_merge($tree, self::sortOut($cate,$v['id'],$level+1,$html)); } } return $tree; } } 这里的$cate是查询上面表获取的一个二维数组:这里就不写了 下面是效果: id name pid level sort 1 PHP 0 1 1 4 --php类 1 2 1 6 ----私有方法 4 3 1 5 --smarty 1 2 2 2 Javascript 0 1 2 7 --php类 2 2 1 3 MySQL 0 1 3 到此,我们就实现了最简单的php无限极分类了,这个在工作中用的很多的。 ---------------------------------------------- 好像不需要array_merge function tree(&$list,$pid=0,$level=0,$html='--'){ static $tree = array(); foreach($list as $v){ if($v['pid'] == $pid){ $v['sort'] = $level; $v['html'] = str_repeat($html,$level); $tree[] = $v; tree($list,$v['id'],$level+1); } } return $tree; } 大家知道分类多了,递归效率也就低了,所以,有了下面的算法:预排序遍历树算法进行无限分类
6. [代码][PHP]代码
public function getTree($menus) { $id = $level = 0; $menuobjs = array(); $tree = array(); $notrootmenu = array(); foreach($menus as $menu) { $menuobj = new stdClass(); $menuobj -> menu = $menu; $id = $menu['id']; $level = $menu['pid']; $menuobj -> nodes = array(); $menuobjs[$id] = $menuobj; if ($level) { $notrootmenu[] = $menuobj; } else { $tree[] = $menuobj; } } foreach($notrootmenu as $menuobj) { $menu = $menuobj -> menu; $id = $menu['id']; $level = $menu['pid']; $menuobjs[$level] -> nodes[] = $menuobj; } return $tree; }