php:简单的面包屑解决方案?

发布于 2024-09-28 20:20:07 字数 660 浏览 6 评论 0原文

嘿伙计们, 我正在研究一个非常简单的面包屑路径解决方案,但是我有一件小事让我烦恼。

PATH 是例如文件夹/子文件夹/子子文件夹 我只是分割路径并创建指向它的链接。真的很简单。

// breadcrumb path
$crumb = explode("/", PATH);
if (PATH != 'root' && realpath(PATH)) {
    print "<div class='breadcrumbs'>";
    $newpath = '';
    foreach($crumb as $value) {
        $newpath .= $value;
        print "<a href='" . QUERY . $newpath ."'>$value</a> &gt; ";
        $newpath .= '/';
    }
    print "</div>";
}

然而,唯一让我烦恼的是面包屑路径看起来像这样:

文件夹>子文件夹>子子文件夹>

你能看到>吗?在最后。即使那里没有另一个子子子文件夹,我得到这个>箭。当然,目前是这样设置的,但是我想不出一个简单的解决方案来摆脱最后一个箭头。

感谢您的帮助

hey guys,
i'm working on a very simple breadcrumb-path solution, however i have one little thing that kind of bugs me.

PATH is e.g. folder/subfolder/subsubfolder
i'm simply splitting the PATH and i'm creating links to it. really simpel.

// breadcrumb path
$crumb = explode("/", PATH);
if (PATH != 'root' && realpath(PATH)) {
    print "<div class='breadcrumbs'>";
    $newpath = '';
    foreach($crumb as $value) {
        $newpath .= $value;
        print "<a href='" . QUERY . $newpath ."'>$value</a> > ";
        $newpath .= '/';
    }
    print "</div>";
}

however the only thing that bugs me is that the breadcrumb path looks like this:

folder > subfolder > subsubfolder >

can you see the > at the end. even though there is not another subsubsubfolder there i'm getting this > arrow. of course it's currently set that way, however i cannot think of an easy solution to get rid of the last arrow.

thank you for your help

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

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

发布评论

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

评论(2

小耗子 2024-10-05 20:20:07

给你:

// breadcrumb path
$crumb = explode("/", PATH);
if (PATH != 'root' && realpath(PATH)) {
    print "<div class='breadcrumbs'>";
    $newpath = '';
    foreach($crumb as $index => $value) {
        $newpath .= $value;
        // is not last item //
        if($index < count($crumb)-1)
            print "<a href='" . QUERY . $newpath ."'>$value</a> > ";
        // it is last item //
        else
            print $value;
        $newpath .= '/';
    }
    print "</div>";
}

同时尝试为变量使用更具暗示性的名称。

Here you go:

// breadcrumb path
$crumb = explode("/", PATH);
if (PATH != 'root' && realpath(PATH)) {
    print "<div class='breadcrumbs'>";
    $newpath = '';
    foreach($crumb as $index => $value) {
        $newpath .= $value;
        // is not last item //
        if($index < count($crumb)-1)
            print "<a href='" . QUERY . $newpath ."'>$value</a> > ";
        // it is last item //
        else
            print $value;
        $newpath .= '/';
    }
    print "</div>";
}

Also try to use more suggestive names for your variables.

白昼 2024-10-05 20:20:07

将您的代码更改为(未经测试!):

// breadcrumb path
$crumb = explode("/", PATH);
if (PATH != 'root' && realpath(PATH)) {
    print "<div class='breadcrumbs'>";
    $newpath = '';
    foreach($crumb as $key=>$value) {
        $newpath .= $value;
        print "<a href='" . QUERY . $newpath ."'>$value</a>";
        if($key!= (count($crumb)-1) )print "> "
        $newpath .= '/';
    }
    print "</div>";
}

Change your code to (not tested!):

// breadcrumb path
$crumb = explode("/", PATH);
if (PATH != 'root' && realpath(PATH)) {
    print "<div class='breadcrumbs'>";
    $newpath = '';
    foreach($crumb as $key=>$value) {
        $newpath .= $value;
        print "<a href='" . QUERY . $newpath ."'>$value</a>";
        if($key!= (count($crumb)-1) )print "> "
        $newpath .= '/';
    }
    print "</div>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文