views:

115

answers:

2

I'm basically trying to grab everything after the 4th segment in the URL, however there is no fixed number of segments. Here is my attempt but for some reason $url is always 0 rather than the string I am looking for. Any suggestions?

$url = '';
for ($counter = 4; $counter <= $this->uri->total_segments(); $counter++) {
    $url += $this->uri->slash_segment($counter);
}
echo $url;
+3  A: 

Try this:

$segs = $this->uri->segment_array(); // get all segments
$my_segs = $segs[3]; // get segments starting from four

foreach ($my_segs as $segment)
{
  echo $segment;
  echo '<br />';
}

Also to concatenate strings, use a dot not plus sign eg:

$url .= $this->uri->slash_segment($counter);
Sarfraz
I can echo the results no problem with both mine and your code. However its where i tried to combine using $url += that i get the problem. It always returns 0.
Alex
@Alex: see my answer again please. Thanks
Sarfraz
i have added one line to my answer at the bottom.
Sarfraz
yeah the problem was using + instead of . doh!!!!!
Alex
@Alex: it is good news then :)
Sarfraz
+1  A: 

You can simplify that code like so:

echo implode('/', array_slice($this->uri->segment_array(), 3));

That will get everything after and including the 4th parameter.

Phil Sturgeon