Breadcrumb

I’m trying to create a breadcrumb which pulls the data from the link and separates out and stylizes. So far I have the following:
[php]<?php
/**
*

*/

class breadcrumbs{

/*
 * @string $breadcrumbs
 */
public $breadcrumbs;

/*
 * @string $pointer
 */
private $pointer = '&raquo;';

/*
 * @string $url
 */
private $url;

/*
 * @array $parts
 */
private $parts;


/*
 * @constructor - duh
 *
 * @access public
 *
 */
public function __construct()
{
    $this->setParts();
    $this->setURL();
    $this->breadcrumbs = '<a href="'.$this->url.'">Home</a>';
}


/*
 *
 * @set the base url
 *
 * @access private
 *
 */
private function setURL()
{
    $protocol = $_SERVER["SERVER_PROTOCOL"]=='HTTP/1.1' ? 'http' : 'https';
    $this->url = $protocol.'://'.$_SERVER['HTTP_HOST'];
}


/*
 * @set the pointer 
 *
 * @access public
 *
 * @param string $pointer
 * 
 */
public function setPointer($pointer)
{
    $this->pointer = $pointer;
}


/**
 *
 * @set the path array
 *
 * @access private
 *
 * @return array
 *
 */
private function setParts()
{
    $parts = explode('/', $_SERVER['REQUEST_URI']);
    array_pop($parts);
    array_shift($parts);
$this->parts = $parts;
}


/**
 *
 * @create the breadcrumbs
 *
 * @access public
 *
  */
public function crumbs()
{
    foreach($this->parts as $part)
    {
        $this->url .= "/$part";
        $this->breadcrumbs .= " $this->pointer ".'<a href="'.$this->url.'">'.$part.'</a>';
    }
}

} /*** end of class ***/

?>

<?php /*** a new breadcrumbs object ***/ $bc = new breadcrumbs; /*** set the pointer if you like ***/ $bc->setPointer('•'); /*** create the trail ***/ $bc->crumbs(); /*** output ***/ echo $bc->breadcrumbs; ?>[/php]

The output appears as

Home • then next page • exctetra.

The odd part is that when you get to a page within our directory which has spaces in the title, the breadcrumb appears as

Home • directory • ##### • business-name - # represents the post number

We would love to have this show as

Home • Directory • Business Name

how would we be able to styleize this so we have capitols on the first letter and no dashes? Additionally if you can figure out the number line, that would be awesome too, but not a priority.

Changing the crumbs function to this should work. Simply didn’t add the value if it’s numeric, and uppercased the first letter of the name. Do note however that this might mess up your URLs, as everything after the numeric value will not get this added to the url.

It should be trivial to add this to the code however if you need this in the url.

[php] public function crumbs() {
foreach ($this->parts as $part) {
if (!is_numeric($part)) {
$this->url .= “/$part”;
$this->breadcrumbs .= " $this->pointer " . ‘’ . ucfirst($part) . ‘’;
}
}
}[/php]

Output:
[php]Home
Directory
Business-name[/php]

any way to get it to list sideways rather then as separate paragraphs? space is limited for the field.

it isn’t outputting seperate paragraphs for me. The code above was the direct html source, there are no paragraphs, no line breaks, only links and dot separators.

Error while parsing PHP: syntax error, unexpected ‘;’, expecting T_FUNCTION

No errors here.

Please add entire working source and entire error message.

Sorry, it would appear to have been a bad copy and paste, I added in those two lines and it looks good. But what about the dashes?

use this to set whatever divider you want

[php]$bc->setPointer(’•’);[/php]

ie:
[php]$bc->setPointer(’-’);[/php]

if you don’t set anything then the default will make it look like this:
Home » Directory » Business-name

Well the output right now is

Home • Directory • Business-name

but I am looking to have it read as:

Home • Directory • Business Name - no dash in the final product and caps on the first letter next in line.

I would edit the crumbs function to suit your wishes.

No dash in final product:
Save the total of $this->parts into a variable.

Change to a for loop so you get an incremental counter, compare the counter to the total variable you made. If you are at the last part then do not add $this->pointer to $this->breadcrumbs
[hr]

caps in all words:
google: php capitalize first letter each word

first hit: http://php.net/manual/en/function.ucwords.php

I just re-read this. For the no dash part you should just do a str_replace to change dashes to spaces

I’m thinking more a preg_replace than a str_replace
$parts = preg_replace(’/[-]+/’, ’ ', $parts);
did the trick… Now… Let’s say I have a multiple word item, such as a business name with multiple words. It reads currently as Hankie hardware store, but would love to have it read as Hankie Hardware Store. I would need to build an array to deal with that kind of change correct? could you show an example?

Would also work, just remember that preg replace is a performance hungry function :slight_smile:

sorry, edited after you responded.

Did you try this?

yeah, I changed ucfirst to ucwords and that fixed it right up. Thanks for the help Jim.

Just a quick off-topic… do you know of a php slideshow example that can grab from two php sources?

Nope, I would just create a proxy for it. As in: just create a small script which fetch x number of sources, merge them together and return.

Sponsor our Newsletter | Privacy Policy | Terms of Service