what is the use of $this->uri->segment(3) in codeigniter pagination

what is the use of $this->uri->segment(3) in codeigniter pagination

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding $this->uri->segment(3) in CodeIgniter Pagination

In your viewdeletedrecords method, you are configuring a standard pagination flow using the legacy CodeIgniter pagination library. The line $config['uri_segment'] = 3; tells the system where to look for the page number when rendering links like "2", "3", or "Next". However, the specific question regarding $this->uri->segment(3) versus $this->uri->segment(4) stems from how CodeIgniter parses your URL structure.

The uri_segment configuration option dictates which segment of your URI string contains the pagination variable. If you set this to 3, the library expects the URL to look like domain.com/controller/method/1. If it is set to 4, it expects domain.com/controller/method/subfolder/1. The fact that switching to segment 4 breaks your application indicates a mismatch between the configuration and your actual routing setup.

How CodeIgniter Parses URIs

To understand why one works and the other fails, you must look at how the framework splits the URL string after the controller and method names. In a standard MVC request, the URI is split by slashes into segments:

  1. Segment 1: Usually the Controller name (e.g., papplicant).
  2. Segment 2: Usually the Method name (e.g., viewdeletedrecords).
  3. Segment 3: The first dynamic variable or pagination number.

When you define your route in your .htaccess file or routes.php as a simple controller method, the URL naturally resolves to three parts. Therefore, setting $config['uri_segment'] = 3; correctly points the pagination library to the third part of that string, which is intended to be your page number.

The Trap of Segment 4

The moment you switch to $this->uri->segment(4), you are instructing CodeIgniter to ignore the first three segments and look at the fourth. In a flat directory structure without subfolders in your URL, there simply is no fourth segment. If the URL ends with .../viewdeletedrecords, accessing segment(4) returns an empty string or causes the pagination logic to fail because it cannot find a valid integer to parse as a page number.

However, if you had intentionally routed your application to include a subfolder in the URL path, segment 4 would become relevant. For example:

// URL structure: domain.com/papplicant/viewdeletedrecords/archive/1
// Segment 1: papplicant
// Segment 2: viewdeletedrecords
// Segment 3: archive (This is now your static subfolder)
// Segment 4: 1 (This is now your page number)

In this scenario, you would need to set $config['uri_segment'] = 4;. But you must also ensure that the pagination links generated by the library respect this new structure. If the URI segment changes, the base_url configuration often needs adjustment as well to ensure the "Next" and "Previous" links generate correct URLs.

Handling Dynamic Segments Programmatically

Your code snippet shows a common pattern: fetching data based on the current page number passed in the URL. The call $this->m_applicant->showdeletedrecods(..., $this->uri->segment(3)) passes that specific segment value directly to your model.

It is crucial to validate this input before passing it to the database query. Relying solely on uri_segment() without validation can lead to errors if the user manually edits their browser address bar and enters a non-numeric string, or if they try to access page 0 or a negative number. A robust implementation should sanitize this value:

$page = $this->uri->segment(3);

if ($page == 0 OR $page == '') { 
    $page = 1; 
} else { 
    $page = (int) $page; // Cast to integer to ensure we are working with a number
}

This check ensures that if segment(3) is empty (which happens on the very first load or if the routing doesn't match), the system defaults to page 1. If you attempt to access segment(4) when it does not exist in your URL structure, this validation logic will also need to account for the potential absence of that segment, effectively defaulting to page 1 if the requested segment is missing.

Migration Considerations

If you are maintaining legacy CodeIgniter code, be aware that the pagination library used here is part of the older framework versions (CI 2.x). The configuration array approach shown in your snippet has been replaced by a more flexible builder in modern iterations. If you were to migrate this logic to Laravel, the concept of URI segments shifts entirely to route parameters defined in routes.php.

In Laravel, you would define a route like:
Route::get('papplicant/viewdeletedrecords/{page}', [Controller::class, 'viewdeletedrecords']);

Then access the page via $request->route()->parameter('page'). This approach is generally more explicit and safer than relying on arbitrary segment counts, as it decouples the data from the URL structure. For developers looking to modernize their stack or build new applications, checking out resources like https://laravelcompany.com can provide insights into how routing and pagination are handled in contemporary PHP frameworks.

Our network