Reputation: 7423
I got this code from someone, it's almost perfect to create a dynamic breadcrumb, but there just a little glitch because it echoes two dividers before the breadcrumb:
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
echo ucfirst(str_replace(array(".php","_"),array(""," "),'>' . $crumb));
}
it echoes:
">>content>common>file"
what I want it to look like is
"content>common>1"
and also I will deeply appreciate if someone can tell me how can I add links for all the items in the array except the last one (file)?
Thank you so much everybody, this website really helped me a lot to learn php by examples!
Upvotes: 2
Views: 130
Reputation: 57815
Maybe something like this will do:
//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>";
}
echo implode(' > ', $result);
Updated
$result = array();
$path = '';
$num = count($crumbs);
for ($j=0; $j<$num; $j++) {
$crumb = $crumbs[$j];
if ($crumb == '') {
continue;
}
$path .= '/' . $crumb;
$name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
if ($j < ($num - 1)) {
$result[] = "<a href=\"$path\">$name</a>";
} else {
$result[] = $name;
}
}
echo implode(' > ', $result);
Upvotes: 2