How to search with pagination in codeigniter?

by cali_green , in category: PHP Frameworks , 6 months ago

How to search with pagination in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 6 months ago

@cali_green 

In CodeIgniter, pagination can be implemented using the built-in Pagination library. Here is a step-by-step guide to enable pagination in your CodeIgniter application:


Step 1: Load the Pagination Library You need to load the Pagination library in your Controller before you can use it. You can load the Pagination library in the constructor function of your Controller like this: $this->load->library('pagination');


Step 2: Configure Pagination You need to configure the Pagination library with your desired settings. You can configure the following settings:


$config['base_url'] = 'http://example.com/controller/function'; $config['total_rows'] = 200; // Total number of items (records) to paginate $config['per_page'] = 10; // Number of items (records) to show per page $config['uri_segment'] = 3; // URI segment that contains the page number


You can set these configurations in the Controller function where you want to display the paginated data.


Step 3: Initialize Pagination You need to initialize the Pagination library with the configuration settings that you have set. You can do this by creating a Pagination object and passing the configuration settings like this:


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


Step 4: Get Paginated Data To get the paginated data to display in your view, you need to fetch the data from the Model and pass it to the view along with the pagination links. You can use the limit() and offset() functions in your Model query to retrieve only the required data for the current page.


Step 5: Display Pagination Links You can display the pagination links in your view using the create_links() function provided by the Pagination library. Place this function where you want to display the pagination links in your view file.


That's it! By following these steps, you can implement pagination in your CodeIgniter application to search with pagination.