溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在php項(xiàng)目中實(shí)現(xiàn)一個(gè)面包屑導(dǎo)航功能

發(fā)布時(shí)間:2020-12-10 15:12:14 來(lái)源:億速云 閱讀:146 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

如何在php項(xiàng)目中實(shí)現(xiàn)一個(gè)面包屑導(dǎo)航功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wè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ù)庫(kù)連接:

<&#63;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());
&#63;>

主函數(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;
}

輸出:

// 簡(jiǎn)單輸出
$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&#63;cid=') { // 也可以組裝
  $res = getPathCate($cateid);
  $str = '';
  foreach($res as $k=>$v) {
    $str.= "<a href='{$link}{$v['id']}'>{$v['catename']}</a> > ";
  }
  return $str;
}
echo displayCatePath(4);

看完上述內(nèi)容,你們掌握如何在php項(xiàng)目中實(shí)現(xiàn)一個(gè)面包屑導(dǎo)航功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI