Error in generating a breadcrumb using PHP?

2

Inquiring on the web, I found two examples that I like.

The first one does not generate errors, it is ideal to create a breadcrumb due to defects, according to the data obtained from the URL.

<?php
$trail = array(
    'path' => 'Cool Stuff',
    'to' => 'Animals',
    'a' => 'Spiders'
);

// You could grab this automatically with $_SERVER['HTTP_HOST']
$url = 'http://example.com/path/to/more/deadly/a/black_widow.php';

$parts         = parse_url($url);
$path          = pathinfo($parts['path']);
$segments      = explode('/', trim($path['dirname'],'/'));

$breadcrumbs[] = '<a href="/">Home</a>';
$crumb_path    = '';

foreach ($segments as $segment)
{
    $crumb_path .= '/' . $segment;

    $value = (array_key_exists($segment, $trail)) ? $trail[$segment] : ucfirst($segment);

    $breadcrumbs[] = '<a href="' . $crumb_path . '">' . $value . '</a>';
}

$breadcrumbs[] = ucwords(str_replace('_', ' ', $path['filename']));
$breadcrumbs   = implode(' &raquo; ', $breadcrumbs);

echo $breadcrumbs;
?>

And as a result the following:

  

Home » Cool Stuff » Animals » More » Deadly » Spiders » Black Widow

I tried it replacing the data of $url , but there are small errors:

For example the URL of my website:

example.com/productos/belleza-mujer/perfume-de-mujer/

Generate the breadcrumb in the following way:

  

Home » Products » Beauty-woman »Perfume-of-woman

But the breadcrumb design should be printed like this:

  

Home » Products » Beauty Woman »Women's Perfume

In the second example, it generates the breadcrumb but executes errors:

<?php
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = array("<a href=\"$base\">".$home."</a>");

    // Find out the index for the last value in our path array
    //$last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"".$base.$crumb."\">".$title."</a>";
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

echo breadcrumbs();
?>

And print the following:

  

Home » Products » Beauty-woman > Women's Perfume

It does not eliminate, the scripts - , in the categories it also keeps the script, the end of the url, generates the link, which it should not.

And the errors are the following:

Notice: Undefined index: HTTPS in C:\xampp\htdocs\tienda\detalle.php on line 15

Notice: Undefined variable: last in C:\xampp\htdocs\tienda\detalle.php on line 29

Which corresponds to:

$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'; //linea 15
    if ($x != $last) //linea 29

The link design should look like this:

    
asked by Fernando 30.07.2018 в 14:43
source

1 answer

0

You can populate like this:

<?php
$trail = array(
   'path' => 'Cool Stuff',
   'to' => 'Animals',
   'a' => 'Spiders'
);

// You could grab this automatically with 
$_SERVER['HTTP_HOST']
$url = 'http://example.com/path/to/more/deadly/a/black_widow.php';

$parts         = parse_url($url);
$path          = pathinfo($parts['path']);
$segments      = explode('/', trim($path['dirname'],'/'));

$breadcrumbs[] = '<a href="/">Home</a>';
$crumb_path    = '';

foreach ($segments as $segment) {
    $crumb_path .= '/' . $segment;

    $value = (array_key_exists($segment, $trail)) ? $trail[$segment] : ucfirst($segment);

    $breadcrumbs[] = '<a href="' . $crumb_path . '">' . str_replace(['_', '-', '+'], ' ', $value) . '</a>';
}

$breadcrumbs[] = ucwords(str_replace(['_', '-', '+'], ' ', 
$path['filename']));
$breadcrumbs   = implode(' > ', $breadcrumbs);

echo $breadcrumbs;
?>

Note that in the function str_replace I put an Array with multiple replace options which are replaced by spaces, in that Array you can add more options in the future

    
answered by 31.07.2018 / 00:18
source