创建“引导栏”像这样,在 php+mysql 的网页上方?

发布于 2024-08-13 08:45:04 字数 400 浏览 6 评论 0原文

我想创建一个“引导栏”(我不知道它的正确名称)。我有一个名为 cat 的表,其中包含以下数据:

catid        catname         parentid
1            news            null
2            sport           1
3            health          1
4            tennis          2
5            football        2
6            soccer          5

我的页面接收:$catid=6,并且我想创建:

news>sport>football>soccer

I want to create a "guide bar" (I don't know its right name). I've got a table called cat that has the following data:

catid        catname         parentid
1            news            null
2            sport           1
3            health          1
4            tennis          2
5            football        2
6            soccer          5

My page receives: $catid=6, and I want to create:

news>sport>football>soccer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经暖 2024-08-20 08:45:04

首先从数据库获取数据并构建一个数组

$q = mysql_query("SELECT catid, catname, parentid FROM cat");
$pages = array();
while($r = mysql_fetch_assoc($q)) {
    $pages[$r['catid']] = array('catname' => $r['catname'], 'parentid' => $r['parentid']);
}

使用递归函数构建面包屑:

function build_breadcrumbs($pages, $catid) {
    if(is_null($catid) || empty($catid)) return;
    return build_breadcrumbs($pages, $pages[$catid]['parentid']).$pages[$catid]['catname'].' > ';
}

该函数返回父级的面包屑+当前的面包屑。使用这样的函数:

$catid = 6;
$breadcrumbs = trim(build_breadcrumbs($pages, $catid), ' >');
echo $breadcrumbs;

或者,如果您更喜欢数组,您可以创建一个这样的函数:

function build_breadcrumbs($pages, $catid) {
    if(is_null($catid) || empty($catid)) return array();
    return array_merge(build_breadcrumbs($pages, $pages[$catid]['parent']), array($pages[$catid]['name']));
}

并将其与 implode 一起使用:

$catid = 6;
$breadcrumbs = implode(' > ', build_breadcrumbs($pages, $catid));
echo $breadcrumbs;

这使您可以选择在函数外部定义分隔符。希望有帮助。

First get your data from database and build an array

$q = mysql_query("SELECT catid, catname, parentid FROM cat");
$pages = array();
while($r = mysql_fetch_assoc($q)) {
    $pages[$r['catid']] = array('catname' => $r['catname'], 'parentid' => $r['parentid']);
}

Use a recursive function to build the breadcrumbs:

function build_breadcrumbs($pages, $catid) {
    if(is_null($catid) || empty($catid)) return;
    return build_breadcrumbs($pages, $pages[$catid]['parentid']).$pages[$catid]['catname'].' > ';
}

The function returns the parent's breadcrumbs + the current breadcrumb. Use the function like this:

$catid = 6;
$breadcrumbs = trim(build_breadcrumbs($pages, $catid), ' >');
echo $breadcrumbs;

Or, if you like arrays more, you can create a function like this:

function build_breadcrumbs($pages, $catid) {
    if(is_null($catid) || empty($catid)) return array();
    return array_merge(build_breadcrumbs($pages, $pages[$catid]['parent']), array($pages[$catid]['name']));
}

and use it with implode:

$catid = 6;
$breadcrumbs = implode(' > ', build_breadcrumbs($pages, $catid));
echo $breadcrumbs;

That gives you the option to define the separator outside the function. Hope that helps.

放飞的风筝 2024-08-20 08:45:04

它们被称为面包屑。这里有一个教程可能会有所帮助: http://www.mindpalette.com/tutorials/面包屑/index.php

They are called bread crumbs. There's a tutorial here that may help: http://www.mindpalette.com/tutorials/breadcrumbs/index.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文