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条)

报告相同问题?