写递归函数,可考虑缓存,定义一些静态变量来存上一次运行的结果,多程序运行效率很有帮助.。
大概步骤如下
step1:到数据库取数据,放到一个数组,
step2:把数据转化为一个树型状的数组,
step3:把这个树型状的数组转为html代码。
也可以将第二步和第三步合为一步。
详细如下:
1、数据库设计:

CREATE TABLE `bg_cate` (
`cate_Id` int(30) unsigned NOT NULL AUTO_INCREMENT,
`cate_ParentId` int(30) unsigned DEFAULT '0',
`cate_Name` varchar(100) NOT NULL,
`cate_Intro` varchar(500) DEFAULT NULL,
`cate_Order` int(30) unsigned DEFAULT '0',
`cate_Icon` varchar(100) DEFAULT NULL,
PRIMARY KEY (`cate_Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=34 ;
--
-- 导出表中的数据 `bg_cate`
--
INSERT INTO `bg_cate` (`cate_Id`, `cate_ParentId`, `cate_Name`, `cate_Intro`, `cate_Order`, `cate_Icon`) VALUES
(4, 0, '往事如风', '记录往事', 0, 'icons/6.gif'),
(5, 0, '水煮三国', '品位三国智慧', 0, 'icons/3.gif'),
(2, 0, '技术学习', '平时学习的一些笔记,欢迎批评指正。', 0, 'icons/18.gif'),
(3, 0, '生活点滴', '记录生活点滴', 0, 'icons/2.gif'),
(6, 0, '栀子花开', '青春无限', 0, 'icons/8.gif'),
(7, 0, '假日休闲', '悠闲、自在', 0, 'icons/24.gif'),
(8, 2, 'html', 'html学习', 0, 'icons/1.gif'),
(9, 2, 'css', 'css学习', 0, 'icons/1.gif'),
(10, 2, 'php', 'php学习', 0, 'icons/18.gif'),
(11, 10, 'php基础知识', 'php基础知识', 0, 'icons/1.gif'),
(12, 10, 'oop', 'oop', 0, 'icons/1.gif'),
(13, 10, 'php安全', '讲述php安全', 0, 'icons/1.gif'),
(14, 10, 'seagull framework', 'seagull framework', 0, 'icons/1.gif'),
(15, 2, 'javascript', 'javascript学习', 0, 'icons/1.gif'),
(16, 2, '设计模式', NULL, 0, 'icons/1.gif'),
(17, 2, '软件工程', '软件工程学习', 0, 'icons/1.gif'),
(18, 3, '厦门生活', '厦门生活', 0, 'icons/8.gif'),
(19, 3, '大学生活', '大学生活', 0, 'icons/8.gif'),
(20, 3, '童年生活', '童年生活', 0, 'icons/15.gif'),
(21, 19, '学习', '学习', 0, 'icons/1.gif'),
(22, 19, '运动', '运动', 0, 'icons/16.gif'),
(23, 19, '旅游', '旅游', 0, 'icons/24.gif'),
(24, 22, '排球', '排球', 0, 'icons/9.gif'),
(25, 22, '篮球', '篮球', 0, 'icons/9.gif'),
(26, 22, '羽毛球', '羽毛球', 0, 'icons/9.gif'),
(27, 22, '乒乓球', '乒乓球', 0, 'icons/9.gif');

2、到数据库取数据,放到数组:

require_once './classes/MyDB.php';
$con = MyDB::singleton();
$sql = <<<SQL
 select * from bg_cate cate
SQL;
$data = $con->getAll($sql);
//print_r($data);

数据库操作我用的是pear类库,最后的$data的数据格式如下:

"htmlcode">
function getTree($data, $pId)
{
$tree = '';
foreach($data as $k => $v)
{
 if($v['cate_ParentId'] == $pId)
 { //父亲找到儿子
 $v['cate_ParentId'] = getTree($data, $v['cate_Id']);
 $tree[] = $v;
 //unset($data[$k]);
 }
}
return $tree;
}
$tree = getTree($data, 0);

最后输出$tree的数据格式为:

"htmlcode">
function procHtml($tree)
{
$html = '';
foreach($tree as $t)
{
 if($t['cate_ParentId'] == '')
 {
 $html .= "<li>{$t['cate_Name']}</li>";
 }
 else
 {
 $html .= "<li>".$t['cate_Name'];
 $html .= procHtml($t['cate_ParentId']);
 $html = $html."</li>";
 }
}
return $html "htmlcode">
function getTree($data, $pId)
{
$html = '';
foreach($data as $k => $v)
{
 if($v['cate_ParentId'] == $pId)
 { //父亲找到儿子
 $html .= "<li>".$v['cate_Name'];
 $html .= getTree($data, $v['cate_Id']);
 $html = $html."</li>";
 }
}
return $html "text-align: center">php创建无限级树型菜单

整个过程思路很清晰,非常适合第一次创建无限级树型的朋友学习,希望大家都有所收获。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。

更新日志