PHP Breadcrumbs

I am trying to find a PHP breadcrmb system that works on InfinityFree. I have found two so far, but each returns a 500 error (In witch I blamed Cloudflare + Logflare for, sorry about that). Is there a reason why that would be happeneing here? I am trying to but them in the <body> section of my PHP file (Other PHP code there works just fine), but it just wont work.

Is there a reason why it is not working, or am I doing something wrong (I really dont think so).

The ones I tryed:

http://stephanieleary.com/2004/01/absurdly-simple-php-breadcrumbs/

Thanks for any suggestions!

For starters, the links you shared are from 2003 and 2004. PHP has changed a lot since then so the code might not be compatible anymore.

It doesn’t seem likely to me that the 500 error is a result of Cloudflare. Cloudflare just forwards whatever the backend server generates, including any errors. Perhaps you could try enabling display_errors and see if you can get an actual error message?

2 Likes

That’s probably why.

Oh, it’s not. I just said I blamed them and took my frustration out on them before finding the real issue was not with them, but with the code.

I found another breadcrumb script that might work, and I’m not on a device with access to my files, so I’ll enable error messages and get back to you when I can.

Coming back here, I enabled errors, and here is what I get.

This one is the one I want to work:
http://stephanieleary.com/2004/01/absurdly-simple-php-breadcrumbs/

The error:

Parse error : syntax error, unexpected ‘crumbs’ (T_STRING), expecting ‘;’ or ‘,’ in /home/vol1000_7/epizy.com/epiz_2xxxxxx7/htdocs/test/other.php on line 2

The code:

<?php
echo "<ul id="crumbs">";
/* get array containing each directory name in the path */
$parts = explode("/", dirname($_SERVER['REQUEST_URI']));
echo "<li><a href="/">Home</a></li>";
foreach ($parts as $key => $dir) {
switch ($dir) {
case "about": $label = "About Us"; break;
/* if not in the exception list above,
use the directory name, capitalized */
default: $label = ucwords($dir); break;
}
/* start fresh, then add each directory back to the URL */
$url = "";
for ($i = 1; $i <= $key; $i++)
{ $url .= $parts&#91;$i&#93; . "/"; }
if ($dir != "")
echo "<li>> <a href="/$url">$label</a></li>";
}
echo "</ul>";
?>

Console is empty


Others:

Parse error : syntax error, unexpected '' (T_NS_SEPARATOR), expecting identifier (T_STRING) in /home/vol1000_7/epizy.com/epiz_28335187/htdocs/test/sitepointone.php on line 9


$path = $_SERVER[“PHP_SELF”]; $parts = explode(‘/’,$path); if (count($parts) < 2) { echo(“home”); } else { echo (“[home](file://%22/index.php//%22) → “); for ($i = 1; $i < count($parts); $i++) { if (!strstr($parts[$i],”.”)) { echo(“[”.$parts[$i].“](file://%22%22%29%3B/) ->”); } else { $str = $parts[$i]; $pos = strrpos($str,“.”); $parts[$i] = substr($str, 0, $pos); echo $parts[$i]; }; }; };

Thanks!

First php line should be:

echo(“<ul id=\“crumbs\”>”);

2 Likes

Not just the first line. Every single line with echo in that example uses " to terminate the PHP strings, and then also uses " to terminate the HTML, which will most definitely break.

You need to either escape the quotes in the PHP string, or mix quotation to prevent this.

So instead of:

echo "<ul id="crumbs">";

You can do:

echo '<ul id="crumbs">';

Or:

echo "<ul id=\"crumbs\">";
1 Like

Cool, fixed that, now getting a parse error with the “if” statement.

Parse error : syntax error, unexpected ‘if’ (T_IF) in /home/vol1000_7/epizy.com/epiz_28335187/htdocs/test/other.php on line 17

(FileMannager not letting me highlight and copy on an iPad, so I just took a screenshot.)

Thanks for the help!

Edit: Adding a “{” at the end on line 17 does not work.

here you have an open but never a closed one

and the second problem is probably in the underlined code

2 Likes

Wasn’t sure how to fix it, so I did my best. Now getting “IF” error.

Parse error : syntax error, unexpected ‘if’ (T_IF) in /home/vol1000_7/epizy.com/epiz_28335187/htdocs/test/other.php on line 17

The #91 etc. part on line 16 are the cause of this.The # is a comment sign, meaning everything after parts& is not interpreted, including the crucial ; and } characters.

I’m shocked that someone would write $parts&#91;$i&#93. I have no idea what it’s supposed to do, and I don’t see how this could have ever worked in PHP.

A quick Google search shows me this example, which looks a lot more modern: PHP - Dynamic Breadcrumbs · GitHub

2 Likes

Probably for this:

https://www.w3.org/MarkUp/html-spec/html-spec_13.html

The main reason I liked this one is because it seems to have the feature to change things. Like if the URL is /hello_hello than the breakcymb could say something different like /hello

Is there a way I could fix it?

The mysterious line

$url .= $parts&#91;$i&#93; . "/";

actually is

$url .= $parts[$i] . "/";

For some strange reason it looks like it went through the htmlentities() php function, or at least part of it did…

p.s. The sorely abused php echo() function, or return() for that matter, by php-ers everywhere, is still a function.
Even though the php parser lets us use it without the including brackets, I always do write the brackets. If it’s a function then call it like one. At least, in my opinion, that makes for more legible code. That’s from an old c/c++ guy.
Also, when you need to escape double quotes, then escape them. Makes you realize what you are actually writing and why. Dont be lazy and use single quotes unless they are necessary. There will come a time when they will be actually necessary (regex).

2 Likes

As you can see, it now partially works…

image

The line

echo "<li> <a href="/$url">$label</a></li>";

should be

echo("<li> <a href=\"/" . $url . "\">" . $label . "</a></li>");
4 Likes

Thank you so much @_recce @Admin and @Oxy! It just needs a bit of CSS and all is good! :tada:

image

1 Like

I just noticed that in your last screenshot, on line 16, you have omitted the dot before the equals sign. You need to have that there. Or, even better, for more legibility, you can write

$url = $url . $parts[$i] . "/";
1 Like

I will add it, but out of curiosity, why?

1 Like

If you don’t have it, every element of the array $parts will overwrite all the previous contents of the $url variable when iterating through the for loop, and you’ll end up with just the last element in the variable, which in this case is a string, representing a url. So the url in each of your breadcrumbs links, will be wrong in the end. It’s the difference between writing

$url = $parts[$i] . "/";

and

$url = $url . $parts[$i] . "/";
1 Like

So, I have the working code:

<?php
echo '<ul>';
/* get array containing each directory name in the path */
$parts = explode("/", dirname($_SERVER['REQUEST_URI']));
echo '<li><a href="/">Home</a>  &raquo;  </li>';
foreach ($parts as $key => $dir) {
switch ($dir) {
case "about": $label = "About Us"; break;
/* if not in the exception list above,
use the directory name, capitalized */
default: $label = ucwords($dir); break;
}}
/* start fresh, then add each directory back to the URL */
$url = "";
for ($i = 1; $i <= $key; $i++){
$url = $parts[$i] . "/";}
if ($dir !="") {
echo("<li> <a href=\"/" . $url . "\">" . $label . "</a>  &raquo;  </li>");
}
echo '</ul>';
?>

Witch exports:

And that is fine (It’s the beginnings what I want), except the path to the file is “/path/to/file/about.html”. Is there a way “About” (The filename) and “To” (Part of the directory) can be added?
Thanks!

Iam not completely certain I understood correctly but perhaps this is what you want.
Copy exactly as is, forget previous code.

<?php
  echo("<ul>");
  /* get array containing each directory name in the path */
  $parts = explode("/", dirname($_SERVER['REQUEST_URI']));
  $filename = basename($_SERVER['REQUEST_URI']);
  echo("<li><a href=\"/\">Home</a>  &raquo; </li>");
  foreach($parts as $key => $dir){
    switch ($dir) {
      case "about": $label = "About Us"; break;
      /* if not in the exception list above, use the directory name, capitalized */
      default: $label = ucwords($dir); break;
    }
  }
  /* start fresh, then add each directory back to the URL */
  $url = "/";
  for ($i = 1; $i <= $key; $i++){
    $url = $url . $parts[$i] . "/";
  }
  if($dir != ""){
    echo("<li> <a href=\"" . $url . "\">" . $label . "</a>  &raquo;</li>");
  }
  
  echo("<li> <a href=\"" . $url . $filename ."\">" . $filename . "</a>  &raquo;</li>");
  echo("</ul>");
?>