php: simple breadcrumb solution? – Education Career Blog

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> &gt; ";
        $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

,

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> &gt; ";
        // it is last item //
        else
            print $value;
        $newpath .= '/';
    }
    print "</div>";
}

Also try to use more suggestive names for your variables.

,

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 "&gt; "
        $newpath .= '/';
    }
    print "</div>";
}

Leave a Comment