您好,欢迎访问一九零五行业门户网

php实现面包屑导航例子分享_php实例

本实例讲解了php实现面包屑导航的方法,面包屑导航在项目非常实用,在此处就写一个这方面的实现。
path表示所有的祖先id,fullpath表示所有的祖先id和本身id
---- 表的结构 `tp_likecate`--create table if not exists `tp_likecate` ( `id` int(10) unsigned not null auto_increment, `catename` varchar(24) not null, `path` varchar(10) not null, `fullpath` varchar(20) not null, primary key (`id`)) engine=innodb default charset=utf8 auto_increment=9 ;
数据
---- 转存表中的数据 `tp_likecate`--insert into `tp_likecate` (`id`, `catename`, `path`, `fullpath`) values(1, '手机', '', ',1'),(2, '功能手机', '1', '1,2'),(3, '老人手机', '1,2', '1,2,3'),(4, '儿童手机', '1,2', '1,2,4'),(5, '智能手机', '1', '1,5'),(6, 'android手机', '1,5', '1,5,6'),(7, 'ios手机', '1,5', '1,5,7'),(8, 'winphoto手机', '1,5', '1,5,8');
数据库连接:

主函数:
function likecate($path='') { // concat() 连接字段 $sql = select id,catename,path, concat(path,',',id) as fullpath from tp_likecate order by fullpath asc; $res = mysql_query($sql); $result = array(); while($row=mysql_fetch_assoc($res)) { $deep = count(explode(',', trim($row['fullpath'], ','))); // explode字符串转换为数组 implode数组转换为字符串 $row['catename'] = @str_repeat(' ', $deep).'|--'.$row['catename']; $result[] = $row; } return $result;}
输出:
// 简单输出$res = likecate();echo ;foreach($res as $key=>$val) { echo {$val['catename']};}echo ;echo
;// 封装方法function getpathcate($cateid) { $sql = select *,concat(path, ',',id) fullpath from tp_likecate where id = $cateid; $res = mysql_query($sql); $row = mysql_fetch_assoc($res); $ids = $row['fullpath']; $sql = select * from tp_likecate where id in($ids) order by id asc; $res = mysql_query($sql); $result = array(); while($row = mysql_fetch_assoc($res)) { $result[] = $row; } return $result;}// 加上了链接的参数function displaycatepath($cateid,$link='cate.php?cid=') { // 也可以组装 $res = getpathcate($cateid); $str = ''; foreach($res as $k=>$v) { $str.= {$v['catename']} > ; } return $str;}echo displaycatepath(4);
效果:
以上就是php实现面包屑导航的详细步骤,希望对大家学习php程序设计有所帮助。
其它类似信息

推荐信息