php實現(xiàn)面包屑導(dǎo)航例子分享
更新時間:2015年12月19日 14:44:41 投稿:lijiao
這篇文章主要為大家分享了php實現(xiàn)面包屑導(dǎo)航例子,感興趣的小伙伴們可以參考一下
本實例講解了php實現(xiàn)面包屑導(dǎo)航的方法,面包屑導(dǎo)航在項目非常實用,在此處就寫一個這方面的實現(xiàn)。

path表示所有的祖先id,fullpath表示所有的祖先id和本身id
-- -- 表的結(jié)構(gòu) `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 ;
數(shù)據(jù)
-- -- 轉(zhuǎn)存表中的數(shù)據(jù) `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');
數(shù)據(jù)庫連接:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'test';
$con = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_name, $con) or die(mysql_error());
mysql_query('set names utf8') or die(mysql_error());
?>
主函數(shù):
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字符串轉(zhuǎn)換為數(shù)組 implode數(shù)組轉(zhuǎn)換為字符串
$row['catename'] = @str_repeat(' ', $deep).'|--'.$row['catename'];
$result[] = $row;
}
return $result;
}
輸出:
// 簡單輸出
$res = likecate();
echo "<select name='cate'>";
foreach($res as $key=>$val) {
echo "<option>{$val['catename']}</option>";
}
echo "</select>";
echo "<br />";
// 封裝方法
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;
}
// 加上了鏈接的參數(shù)
function displayCatePath($cateid,$link='cate.php?cid=') { // 也可以組裝
$res = getPathCate($cateid);
$str = '';
foreach($res as $k=>$v) {
$str.= "<a href='{$link}{$v['id']}'>{$v['catename']}</a> > ";
}
return $str;
}
echo displayCatePath(4);
效果:

以上就是php實現(xiàn)面包屑導(dǎo)航的詳細步驟,希望對大家學習php程序設(shè)計有所幫助。
相關(guān)文章
tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法
這篇文章主要介紹了tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法,結(jié)合實例形式簡單分析了mongoDB數(shù)據(jù)庫及thinkPHP5連接、查詢MongoDB數(shù)據(jù)庫的基本操作技巧,需要的朋友可以參考下2018-01-01
PHP實現(xiàn)QQ登錄的開原理和實現(xiàn)過程
本文主要給大家講述了PHP實現(xiàn)QQ登錄的開原理以及相關(guān)的代碼實現(xiàn)過程,對此有興趣和需要的朋友參考下吧。2018-02-02
在 Laravel 6 中緩存數(shù)據(jù)庫查詢結(jié)果的方法
這篇文章主要介紹了在 Laravel 6 中緩存數(shù)據(jù)庫查詢結(jié)果的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
利用Fix Rss Feeds插件修復(fù)WordPress的Feed顯示錯誤
這篇文章主要介紹了利用Fix Rss Feeds插件修復(fù)WordPress的Feed顯示錯誤的方法,需要的朋友可以參考下2015-12-12

