dougou6114 2013-01-01 23:42
浏览 31
已采纳

动态面包屑没有看到嵌套文件夹

I used the code found here for dynamic php breadcrumbs for my website. It works great! however, If I nest a folder more than 1 deep, It causes errors.

Here's the code that I have currently for the breadcrumbs.

    <?php
    function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
        $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
        $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
        $breadcrumbs = Array("<a href=\"$base\">$home</a>");
        $last = end(array_keys($path));
        foreach ($path AS $x => $crumb) {
            $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
            if ($x != $last)
                $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
            else
                $breadcrumbs[] = $title;
        }
        return implode($separator, $breadcrumbs);
     }
    ?>
    You are here: <?= breadcrumbs(' ♥ ') ?>

The easiest place to see a live example is here. If you click on the third link on the breadcrumbs, it ignores the second nested folder. I don't know enough PHP to trouble shoot the problem and how to fix it. I would think ideally it would watch for nested folders in the url.

  • 写回答

3条回答 默认 最新

  • duanjiagu0655 2013-01-01 23:50
    关注

    You need to add previous crumbs to get it working correctly. Here's a fix:

    <?php
    function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
        $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
        $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
        $breadcrumbs = Array("<a href=\"$base\">$home</a>");
        $last = end(array_keys($path));
        foreach ($path AS $x => $crumb) {
            $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
            if ($x != $last)
                $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
            else
                $breadcrumbs[] = $title;
            $base .= $crumb . '/';
        }
        return implode($separator, $breadcrumbs);
     }
    ?>
    You are here: <?= breadcrumbs(' ♥ ') ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改