Codeigniter-breadcrumbs
Codeigniter-breadcrumbs copied to clipboard
Unshift crash breadcrumb
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;
}
}
Could you explain why it doesn't work fine with a case?
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:
- Controller file (the login function is called first, then the index function)
public function index() {
...
$this -> breadcrumbs -> unshift('Home', '/');
$data['breadcrumbs'] = $this -> breadcrumbs -> show();
...
}
public function login() {
...
$this -> breadcrumbs -> push('Login', '/account/login');
...
}
Original show function:
- Result:
<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:
- Result:
<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>
// 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