欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php创建无限级树型菜单

程序员文章站 2023-11-14 14:47:22
写递归函数,可考虑缓存,定义一些静态变量来存上一次运行的结果,多程序运行效率很有帮助.。 大概步骤如下: step1:到数据库取数据,放到一个数组, step2:把数...

写递归函数,可考虑缓存,定义一些静态变量来存上一次运行的结果,多程序运行效率很有帮助.。
大概步骤如下
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的数据格式如下:

array
(
 [0] => array
 (
 [cate_id] => 4
 [cate_parentid] => 0
 [cate_name] => 往事如风
 [cate_intro] => 记录往事
 [cate_order] => 0
 [cate_icon] => icons/6.gif
 )
 [1] => array
 (
 [cate_id] => 5
 [cate_parentid] => 0
 [cate_name] => 水煮三国
 [cate_intro] => 品位三国智慧
 [cate_order] => 0
 [cate_icon] => icons/3.gif
 )

3、把上一步的数据转为树型状的数组代码如下:

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的数据格式为:

array
(
 [0] => array
 (
 [cate_id] => 4
 [cate_parentid] =>
 [cate_name] => 往事如风
 [cate_intro] => 记录往事
 [cate_order] => 0
 [cate_icon] => icons/6.gif
 )
 [1] => array
 (
 [cate_id] => 5
 [cate_parentid] =>
 [cate_name] => 水煮三国
 [cate_intro] => 品位三国智慧
 [cate_order] => 0
 [cate_icon] => icons/3.gif
 )
 [2] => array
 (
 [cate_id] => 2
 [cate_parentid] => array
 (
  [0] => array
  (
  [cate_id] => 8
  [cate_parentid] =>
  [cate_name] => html
  [cate_intro] => html学习
  [cate_order] => 0
  [cate_icon] => icons/1.gif
  )

4、把树型状数组转为html代码如下:

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 ? '<ul>'.$html.'</ul>' : $html ;
}
echo prochtml($tree);
输出的html的代码格式为:
<ul>
<li>往事如风</li>
<li>水煮三国</li>
<li>技术学习
 <ul>
 <li>html</li>
 <li>css</li>
 <li>php
 <ul>
 <li>php基础知识</li>
 <li>oop</li>
 <li>php安全</li>

5、也可以把第3和第4步的代码合在一起,代码如下:

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 ? '<ul>'.$html.'</ul>' : $html ;
}
echo gettree($data, 0);

6、最后再加点css样式,效果如下:

php创建无限级树型菜单

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