• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solved: How to Dynamic breadcrumbs based on uri segment??

#1
[eluser]CtheB[/eluser]
Hi,

Does somebody know somethin' like a helper for dynamic breadcrumbs based on uri?
Somethin' like this:
(But then smarter and less code. So you can have unlimited breadcrumbs.)
Code:
<?php
        echo anchor(base_url(), $this->system->site_name);
        for ($i = 1; $i <= 5; $i++)
        {
            if($this->uri->segment($i) != '' )
            {
                if($this->uri->segment(2) != '' && $i == 2)
                {
                    $this->seg1= $this->uri->segment(1).'/';
                }
                else
                {
                    $this->seg1= '';
                }
                if($this->uri->segment(3) != '' && $i == 3)
                {
                    $this->seg1= $this->uri->segment(1).'/';
                    $this->seg2= $this->uri->segment(2).'/';
                }
                else
                {
                    $this->seg2= '';
                }
                if($this->uri->segment(4) != '' && $i == 4)
                {
                    $this->seg1= $this->uri->segment(1).'/';
                    $this->seg2= $this->uri->segment(2).'/';
                    $this->seg3= $this->uri->segment(3).'/';
                }
                else
                {
                    $this->seg3= '';
                }
            if($this->uri->segment(5) != '' && $i == 5)
                {
                    $this->seg1= $this->uri->segment(1).'/';
                    $this->seg2= $this->uri->segment(2).'/';
                    $this->seg3= $this->uri->segment(3).'/';
                    $this->seg4= $this->uri->segment(4).'/';
                }
                else
                {
                    $this->seg4= '';
                }
                echo '  >>  ';
                echo anchor(base_url().$this->seg1.$this->seg2.$this->seg3.$this->seg4.$this->uri->segment($i), $this->uri->segment($i));
            }
        }
?&gt;

I tried to rewrite it with more for loops in it. but i can't figure it out yet:

Code:
&lt;?php

echo anchor(base_url(), $this->system->site_name);
        for ($i = 1; $i <= 5; $i++)
        {
            if($this->uri->segment($i) != '' )
            {
                for ($j = 1; $j <= 5; $j++)
                {
                    for($k = 1; $k == $j; $k++)
                    {
                        if($this->uri->segment($j+1) != '' && $i == $j+1)
                        {
                            $seg[$k] = $this->uri->segment($k).'/';
                        }
                        else
                        {
                            $seg[$k] = '';
                        }
                    }
                }
                foreach($seg as $se)
                {
                    $this->theseg .= $se;
                }
                echo '  >>  ';
                echo anchor(base_url().$this->theseg.$this->uri->segment($i), $this->uri->segment($i));
            }
        }
?&gt;
(echoes only first and last uri segment, not the uri segments in between...)

Thnx for helping me out!

Greetz Carlos

#2
[eluser]bitist[/eluser]
Not sure, but if I understand you want something to regenerate the uri without the empty parts.
If yes try this code:
Code:
$new_uri='';
$uri_string = explode("/", $this->uri->uri_string);
foreach ($uri_string as $value) {
    if(trim($value)!="")
        $new_uri .= $value."/";
}
echo anchor(base_url()."/".$new_uri;

#3
[eluser]xwero[/eluser]
Code:
$urls = array();
$segments = $this->uri->segment_array();
foreach($segments as $key=>$segment)
{
    $urls[] = array(site_url(implode( '/',array_slice($segments,0,$key+1))),$segment);
}
So http://site.com/controller/method should give you
Code:
array( array('http://site.com/controller','controller'), array('http://site.com/controller/method','method'))
Which you can display in different ways.

#4
[eluser]CtheB[/eluser]
Thank you xwero,

That's exactly where i was searching for.. the uri segment_array() doh!
I should had looked better in the user guide prob..
I made only 1 small adjustment:
Code:
$urls = array();
$segments = $this->uri->segment_array();
foreach($segments as $key=>$segment)
{
    $urls[] = array(site_url(implode( '/',array_slice($segments,0,$key))),$segment);
}
I removed the +1 after the $key in the array_slice, don't know why you added that...
Now i can echo the content so easilySmile

Code:
foreach($urls as $key => $value)
        {
            echo '  >>  ';
            echo anchor($value[0], ucfirst($value[1]));
        }

Thanks againSmile

PS. Devpedia thanx for helping, but xwero's alternative was where i was searching for..

#5
[eluser]bd3521[/eluser]
Sweet thanks. Just have to remove the echo ' >> '; prior to the first segment.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.