nobuti / Codeigniter-breadcrumbs

Small breadcrumb library for CodeIgniter
70 stars 68 forks source link

Unshift crash breadcrumb #7

Open tjapro opened 9 years ago

tjapro commented 9 years ago

When unshift a breadcrumb, the final result doesn't work fine. Possible resolution:

// construct output
            /*
            foreach ($this->breadcrumbs as $key => $crumb) {
                $keys = array_keys($this->breadcrumbs);
                if (end($keys) == $key) {
                    $output .= $this->crumb_last_open . '' . $crumb['page'] . '' . $this->crumb_close;
                } else {
                    $output .= $this->crumb_open.'<a href="' . $crumb['href'] . '">' . $crumb['page'] . '</a> '.$this->crumb_divider.$this->crumb_close;
                }
            }*/
// newer construct output
            $num = count($this->breadcrumbs);
            $i = 0;
            foreach ($this->breadcrumbs as $key => $crumb) {
                if (++$i === $num) {
                    $output .= $this->crumb_last_open . '' . $crumb['page'] . '' . $this->crumb_close;
                } else {
                    $output .= $this->crumb_open.'<a href="' . $crumb['href'] . '">' . $crumb['page'] . '</a> '.$this->crumb_divider.$this->crumb_close;
                }
            }
nobuti commented 9 years ago

Could you explain why it doesn't work fine with a case?

tjapro commented 9 years ago

I'm sorry for the confusion, but in the title of the issue, isn't the unshift function, but the show function. So... When I tried do a breadcrumb (after use the unshift function), the function show all the parts in active mode. After a research in the code, the failure is inside the foreach cycle. The if don't make the difference between an intiger $key to a href $key (because the php array_unshift function). So the solution, is verify every foreach cycle if the next cycle exist; if not isn't the last breadcrumb. Screenshots:

public function index() {
...
        $this -> breadcrumbs -> unshift('Home', '/');
        $data['breadcrumbs'] = $this -> breadcrumbs -> show();
...
}
    public function login() {
        ...

        $this -> breadcrumbs -> push('Login', '/account/login');
        ...
    }

Original show function:

<section class="col-md-4 col-lg-3">
            <nav>
                <ol class="breadcrumb"><li class="active">Home</li><li class="active">About</li></ol>
            </nav>
        </section>

Modified show function:

        <section class="col-md-4 col-lg-3">
            <nav>
                <ol class="breadcrumb"><li><a href="http://localhost/tp/foodbasket/">Home</a> </li><li class="active">About</li></ol>
            </nav>
        </section>
JonAlco commented 8 years ago

// newer construct output foreach ($this->breadcrumbs as $key => $crumb) { if (++$i === $num) { $output .= $this->crumb_last_open . ' ' . $crumb['page'] . '' . $this->crumb_close; } else { $output .= $this->crumb_open.'<a href="' . $crumb['href'] . '">' . $crumb['page'] . '</a> '.$this->crumb_divider.$this->crumb_close; } } RESULTADO -> home / / Usuario // new construct output foreach ($this->breadcrumbs as $key => $crumb) { if (++$i === $num) { $output .= /*$this->crumb_last_open .*/ ' ' . $crumb['page'] . '' . $this->crumb_close; } else { $output .= $this->crumb_open.'<a href="' . $crumb['href'] . '">' . $crumb['page'] . '</a> '.$this->crumb_divider.$this->crumb_close; } }

RESULTADO -> home / Usuario