offset codeigniter 3

Step 1: Load the Pagination Library

In order to use the offset feature in CodeIgniter, you need to load the Pagination library. This library provides the necessary functions and methods to handle pagination in your application. To load the Pagination library, you can use the following code:

$this->load->library('pagination');

Step 2: Configure Pagination Settings

After loading the Pagination library, you need to configure the pagination settings. This includes setting the base URL, total number of items, items per page, and other optional settings. Here is an example of how you can configure the pagination settings:

$config['base_url'] = 'http://example.com/index.php/controller/method';
$config['total_rows'] = 200;
$config['per_page'] = 10;

$this->pagination->initialize($config);

In the above code, the base_url represents the URL of the page where the pagination links will be displayed. The total_rows is the total number of items you have in your data set. The per_page is the number of items to be displayed per page.

Step 3: Retrieve Data with Offset

Once the pagination settings are configured, you can retrieve the data with the offset. The offset represents the starting point of the data to be displayed on the current page. To retrieve the data, you can use the limit and offset functions of the database query.

Here is an example of how you can retrieve data with the offset:

$limit = $config['per_page'];
$offset = $this->uri->segment(3);

$this->db->limit($limit, $offset);
$query = $this->db->get('table_name');

foreach ($query->result() as $row) {
    // Process each row of data
}

In the above code, the $limit variable holds the number of items to be displayed per page. The $offset variable is obtained from the third segment of the URL, which represents the starting point of the data on the current page. The limit and offset functions are used to set the limit and offset in the database query. Finally, the data is retrieved using the get function and processed in the foreach loop.

Step 4: Display Pagination Links

To display the pagination links, you can use the create_links function provided by the Pagination library. This function generates the HTML markup for the pagination links based on the configured settings. Here is an example of how you can display the pagination links:

echo $this->pagination->create_links();

In the above code, the create_links function generates the HTML markup for the pagination links. You can simply echo this function to display the pagination links on your page.

Step 5: Handle User Clicks on Pagination Links

When a user clicks on a pagination link, you need to handle the click event and update the offset value accordingly. This can be done by passing the offset value as a parameter in the URL. Here is an example of how you can handle user clicks on pagination links:

public function method($offset = 0) {
    // Update the offset value based on user click
    // Retrieve data and display on the page
}

In the above code, the method function is the controller method that handles the pagination. The $offset parameter represents the offset value, which is obtained from the URL. You can update the offset value based on the user click and retrieve the data accordingly.

Conclusion

By following these steps, you can implement pagination with offset in CodeIgniter 3. The Pagination library provides the necessary functions and methods to handle pagination, and you can configure the pagination settings, retrieve data with the offset, display the pagination links, and handle user clicks on the pagination links.