views:

23

answers:

2

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

A: 

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>";
}
Anzeo
+1  A: 

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.

Alin Purcaru
thank you very much. and how about unlinking the last item as well. so now i have "folder > subfolder > subsubfolder" and all three items are linked. is it possible to unlink the last item? because it doesn't make sense to have the last item linked, it just links to itself!
See the edit. Just compare against the length of the `$crumb` array to see which item it is. `count($crumb)` being how you get the length of an array in PHP.
Alin Purcaru