Welcome Guest, Not a member yet? Register   Sign In
Breadcrumbs
#1

[eluser]KeyStroke[/eluser]
I'm trying to include flexible and automated breadcrumbs in my application, but I have no idea where to start.

I've found this thread and this one also, but both seem too complicated, and don't offer enough customization.

Is there a better library that can handle this?

If not, will at least someone help me in nailing the concept of breadcrumbs within CodeIgniter?


Appreciate your help Smile
#2

[eluser]xwero[/eluser]
The easiest solution is a db table that holds all your links with their hierarchy, and you fetch the current link which builds the crumb trail.
#3

[eluser]KeyStroke[/eluser]
I've thought of that, but the problem is that my breadcrumb is dynamic. Meaning, it could be :

Home > cars > topic

or

Home > games > topic

Just like this forum's breadcrumb.

How would that work?
#4

[eluser]richthegeek[/eluser]
all you need is a breadcrumb array (or write a helper)

In the CI index.php, add
Code:
$breadcrumb = new array( '/'=>'Home' );

Then for each seperate level of the site add another key to the breadcrumb array, something like

Code:
class Faq extends Controller {

     function Faq() {
       parent::Controller();
       $breadcrumb['faq'] = 'FAQ';
     }
    
     function index() {
        // get top level categories
        $breadcrumb['faq'] = 'Root';
     }

     function view( $category ) {
        // get info of category;
        $breadcrumb['faq/view/'.$category] = $category_info->title;
     }
}

That sort of thing.

In your view, you can then call another view called "breadcrumb.php", which loops through this array creating a breadcrumb trail.

If you had a helper, you could do easier stuff like:
Code:
$this->breadcrumb->add( URL, name );
Code:
$this->breadcrumb->draw( [return] );
#5

[eluser]xwero[/eluser]
It doesn't matter how you name the links, they should all have a unique number.

Or another way you could go is to identify the trail based on the uri_string.
#6

[eluser]Pascal Kriete[/eluser]
[quote author="xwero" date="1208281002"]It doesn't matter how you name the links, they should all have a unique number.

Or another way you could go is to identify the trail based on the uri_string.[/quote]

Which could look something like this (using modules here, but it would be easy enough to revert to folders):
Code:
function from_path($slug = '')
    {
        $link = base_url();
        $route =& load_class('Router');
        
        if ($route->module != 'home')
        {
            $link .= $route->module;
            $this->_path .= $this->_delim.anchor($link, ucfirst($route->module));
        }
        
        if ($route->class != $route->module)
        {
            $link .= '/'.$route->class;
            $this->_path .= $this->_delim.anchor($link, ucfirst($route->class));
        }
        
        if ($route->method != 'index')
        {
            $link .= '/'.$route->method;
            $this->_path .= $this->_delim.anchor($link, ucfirst($route->method));
        }
        
        $this->_path .= $slug !== '' ? $this->_delim . $slug : '';
        $this->_output();
    }

Where the _output function displays it any way you want. In my case, I just add it to the ci output buffer.
#7

[eluser]KeyStroke[/eluser]
Thanks guys. But to be honest, those examples kind of confused me. Maybe because I can't see the implementation or something, but it wasn't clear for me.

Maybe there's another way around? 8-/
#8

[eluser]frenzal[/eluser]
There are too many ways to do this, it heavily depends on where you get your page data from imho
#9

[eluser]Pascal Kriete[/eluser]
Quite correct, in some instances you may even have to iterate through the database (not fun).

Can you give an example of the kind of breadcrumb you need. What part wasn't clear to you? What kind of application do you want to use it in?
#10

[eluser]adamp1[/eluser]
I use a manual method to create breadcrumbs becasue otherwise its so hard to get something worth while out of the uri_string alone. Also it means you can have complex breadcrumbs. Just use an array to store the crumbs and then as said above a display method.

Code:
<?php
  class Breadcrumb
  {
    var $breadcrumb_trail = array();
    /**
     * Set Breadcrumb
     *
     * @access public
     * @param string $name Name of crumb
     * @param string $link CI Controller link e.g. auth/login
     * @return void
     */
    function set_crumb($name, $link = '')
    {
        $this->breadcrumb_trail[$name] = $link;
        log_message('debug','Breadcrumb link "'.$name.'" pointing to "'.$link.'" created');
        return;
    }

    /**
     * Output Breadcrumb Trail
     *
     * @access public
     * @param boolean $print Prints string to page instead of returning it
     * @return string
     */
    function output_breadcrumb($print = TRUE)
    {
        $output = "";

        $i = 1;
        foreach ( $this->breadcrumb_trail as $name => $link )
        {
            if ( $i == count($this->breadcrumb_trail) ) {
                // On last item, only show text
                $output .= $name;
            } else {
                $output .= anchor($link, $name);
                $output .= " » ";
            }
            $i++;
        }

        // Print/Output trail
        if ($print){
            print $output;
            return;
        }
        return $output;
    }
  }
?>

That's my library I use. So just save that as a file in the libraries folder and use the functions in there. Due to the way its made you can build the trail up as you go along.




Theme © iAndrew 2016 - Forum software by © MyBB