2

I'm trying to create a breadcrumb menu via PHP and have the following so far:

<?php

// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = 'http://'.$_SERVER['HTTP_HOST'];

// 2. Strip extras
foreach($crumbs as $crumb){
    $crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
    echo "<a href=".$address.">"."<span class='crumbMenu'>".$crumb."</span></a>";
}

?>

Let's say I have the following page hierarchy: Products > Products Level 2 > Products Level 3

The above code will spit out:

Products
Products Level 2
Products Level 3

Which is correct. However, the links are not.

After reading up on HTTP_HOST, I'm certain my approach is wrong, but unsure on what other approach I can take to dynamically get each crumb items link?

Links I am getting:

localhost:8080

Links I am expecting:

  • Products: /products

  • Products Level 2: /products/products-level-2

  • Products Level 3: /products/products-level-2/products-level-3

Freddy
  • 683
  • 4
  • 35
  • 114
  • 1
    What links are you getting. Can we get some examples. – NashPL Aug 05 '19 at 13:49
  • 1
    You have the parts of the URL path already (in $crumbs) - you just need to assemble them correctly again. To correctly link to the second path segment in `/foo/bar`, you of course need to not link to `/bar` only, but you need to keep the previous segments as “prefixes”. – misorude Aug 05 '19 at 14:00

2 Answers2

4

You seem to have forgotten about adding routes to $address variable, so all your breadcrumbs point to base server address. Try the following:

<?php

// 1. Get URL
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
$address = 'http://'.$_SERVER['HTTP_HOST'];

// 2. Strip extras
$build = $address.'/products';
foreach($crumbs as $crumb){
    if(in_array($crumb, [$_SERVER['HTTP_HOST'], 'products'])) {
        continue;
    }
    $build .= '/'.$crumb;
    $crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
    echo "<a href=".$address.$build.">"."<span class='crumbMenu'>".$crumb."</span></a>";
}

?>
Lukasz Formela
  • 447
  • 4
  • 17
  • better than my :D – NashPL Aug 05 '19 at 14:05
  • Perfect! has the correct links showing up now. Would you happen to know why my for loop is spitting out additional links? I.e. I expect it to only display three links in inspect mode, but it's show five (`localhost:8080` , `/products` , `/products/products-level-2/` , `/products/products-level-2/products-level-3` and `/products/products-level-2/products-level-3` again? You've already answered the original question so happy to resolve this thread if otherwise – Freddy Aug 05 '19 at 14:18
  • I have updated my answer, it's not ideal solution but will work in your case. – Lukasz Formela Aug 05 '19 at 15:00
  • 1
    Ah, I see the approach, but looking for a more dynamic way to approach this. I'm marking this complete since you've already solved my original question, thanks again – Freddy Aug 07 '19 at 06:41
0

It is very hard to ready your echo which is not helpful. I created small function which should do what you have asked for. Feel free to modify it for your needs.

$crumbs = "/x/y/z";
$address = "localhost:8080";

$crumbs = explode('/', $crumbs);
$end = '';
$href = '';
foreach ($crumbs as $crumb) {
    $crumb = str_replace('.php', '', $crumb);
    $end .= $crumb . ' ';
    $href .= $crumb . '/';
}

echo("HREF => " . $href);
echo("\n");
echo("TITLE => " . $end);
NashPL
  • 461
  • 1
  • 4
  • 19