开发者

PHP Dynamic Breadcrumb

I got this code from someone and it works very well, I just want to remove the link from the last element of the array:

//get rid of empty parts
$crumbs = array_filter($crumbs);

$result = array();
$path = '';
foreach($crumbs as $crumb){
    $path .= 开发者_运维技巧'/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    $result[] = "<a href=\"$path\">$name</a>";

}

print implode(' > ', $result);

This will output for example: Content > Common > File

I just want a to remove the link from the last item - "File" to be just plain text.. I tried myself to count the array items and then if the array item is the last one then to print as plain text the last item.. but I'm still noob, I haven't managed to get a proper result..

Thank you!


This should work:

$crumbs = array_filter($crumbs);

$result = array(); $path = '';
//might need to subtract one from the count...
$count = count($crumbs);
foreach($crumbs as $k=>$crumb){
     $path .= '/' . $crumb;
     $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
     if($k != $count){
         $result[] = "<a href=\"$path\">$name</a>";
     } else {
         $result[] = $name;
     }
}

print implode(' > ', $result);


You could simply tweak your existing code to use a 'normal' loop (rather than a foreach iterator) to achieve this.

For example:

//get rid of empty parts
$crumbs = array_filter($crumbs);

$result = array();
$path = '';
$crumbCount = count($crumbs);
for($crumbLoop=0; $crumbLoop<$crumbCount; $crumbLoop++) {
    $path .= '/' . $crumbs[$crumbLoop];
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumbs[$crumbLoop]));
    $result[] = ($crumbLoop != $crumbCount -1) ? "<a href=\"$path\">$name</a>" : $name;
}

print implode(' > ', $result);

N.B: I don't have access to PHP at the moment, so the above might not be error free, but you should get the idea.


for($i=0;$i< sizeof($crumbs);$i++) {
   $path .= '/' . $crumbs[$i];
   $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumbs[$i]));

   if ($i != sizeof($crumbs)-1) {
      $result[] = "<a href=\"$path\">$name</a>";
   }else {
      $result[] = $name;
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜