'CodeIgniter - Get Last URI Segment
I'm trying to get the last URI segment in CI, however I don't know what the number for it will be, as parameters (an integer) will be appended as the user clicks links within the page. These are then used in a controller to pull relevant database records into the page via ajax.
How can I tell CI to get the last segment?
Something like:
$record_num = $this->uri->segment($last);
Solution 1:[1]
This should work:
$last = $this->uri->total_segments();
$record_num = $this->uri->segment($last);
Solution 2:[2]
I needed this in 2021 and here's how I got to it
$record_num = end($this->uri->segments)
Ty to Anpher that got me in the right track
Solution 3:[3]
Try this :
$last_segment = $this->uri->segment($this->uri->total_segments());
echo $last_segment;
Above code is for English language, for UTF8 such as Arabic, Persian, etc use :
$last_segment = $this->uri->segment($this->uri->total_segments());
echo urldecode($last_segment);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mischa |
| Solution 2 | Paulo Prata |
| Solution 3 | Ahmadreza Sadafi |
