欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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, '手機(jī)', '', ',1'),
(2, '功能手機(jī)', '1', '1,2'),
(3, '老人手機(jī)', '1,2', '1,2,3'),
(4, '兒童手機(jī)', '1,2', '1,2,4'),
(5, '智能手機(jī)', '1', '1,5'),
(6, 'android手機(jī)', '1,5', '1,5,6'),
(7, 'IOS手機(jī)', '1,5', '1,5,7'),
(8, 'WinPhoto手機(jī)', '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('&nbsp;&nbsp;', $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)航的詳細(xì)步驟,希望對大家學(xué)習(xí)php程序設(shè)計有所幫助。

相關(guān)文章

最新評論