PHP 动态面包屑导航

发布于 2024-08-28 06:59:46 字数 534 浏览 4 评论 0原文

我从某人那里得到了这段代码,它运行得很好,我只想删除数组最后一个元素中的链接:

//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);

这将输出例如: 内容>>常见>文件

我只想删除最后一项的链接 - “文件”只是纯文本.. 我尝试自己计算数组项,然后如果数组项是最后一项,则以纯文本形式打印最后一项..但我仍然是菜鸟,我还没有设法获得正确的结果..

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2024-09-04 06:59:46

这应该有效:

$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);

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);
彩虹直至黑白 2024-09-04 06:59:46

您可以简单地调整现有代码以使用“正常”循环(而不是 foreach 迭代器)来实现此目的。

例如:

//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);

注意:我目前无法访问 PHP,因此上面的内容可能并非没有错误,但您应该明白了。

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.

述情 2024-09-04 06:59:46
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;
   }
}
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;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文