CodeIgniter Forums
Solved: How to Dynamic breadcrumbs based on uri segment?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Solved: How to Dynamic breadcrumbs based on uri segment?? (/showthread.php?tid=14729)



Solved: How to Dynamic breadcrumbs based on uri segment?? - El Forum - 01-13-2009

[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


Solved: How to Dynamic breadcrumbs based on uri segment?? - El Forum - 01-14-2009

[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;



Solved: How to Dynamic breadcrumbs based on uri segment?? - El Forum - 01-14-2009

[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.


Solved: How to Dynamic breadcrumbs based on uri segment?? - El Forum - 01-14-2009

[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..


Solved: How to Dynamic breadcrumbs based on uri segment?? - El Forum - 01-30-2009

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