Alex
Alex

Reputation: 6665

Building URL in CodeIgniter

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;

Upvotes: 0

Views: 396

Answers (2)

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382881

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

Upvotes: 3

Related Questions