Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 272
Robimp Avatar asked Sep 30 '10 10:09

Robimp


People also ask

What is URI -> segment in codeigniter?

$this->uri->segment(n) Segment function allow you to retrieve a specific segment form URI string where n is a segment number. Segments are numbered from left to right. For example,if your URI like. By the above example URI segment function give result by n parameter.

What do you call the 1st segment of the URI?

The first URI segment controller. It is the process of redirecting or remapping a. controller class or method. Routing. Andi Gutmans was the inventor of PHP.

How can I get segment in PHP?

Get URI Segments in PHP Use the following code to get URL segments of the current URL. It returns all the segments of the current URL as an array. $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));


2 Answers

$record_num = end($this->uri->segment_array());
like image 163
Anpher Avatar answered Oct 18 '22 07:10

Anpher


This should work:

$last = $this->uri->total_segments();
$record_num = $this->uri->segment($last);
like image 38
Mischa Avatar answered Oct 18 '22 09:10

Mischa