Ajax Pagination Using CodeIgniter and MySQL

Pagination is one of the most frequently used features for web applications. In this post, We have share how to implement Ajax Pagination Using CodeIgniter and MySQL and enhance it further as per our need. Pagination is the process of dividing a document into discrete pages. This is a very simple example, you can just copy paste, and change according to your requirement.
Before started to implement the Ajax Pagination Using CodeIgniter and MySQL, look files structure:
  • ajax-pagination-using-codeigniter-mysql
    • application
      • config
        • autoload.php
        • database.php
        • constants.php
        • pagination.php
        • routes.php
      • controllers
        • Employee.php
      • models
        • Employee_model.php
      • views
        • employee
          • index.php
        • templates
          • header.php
          • footer.php
    • system
    • index.php
    • vendor
    • assets
      • css
        • style.css
Step 1: Create the database and Table
For this tutorial, you need a MySQL database with the following table:
Step 2: Initialization CodeIgniter pagination library
Initialization CodeIgniter pagination library (file name: pagination.php) “application/config/” folder.
  • per_page: Refers to the number of how many entries will be shown on each page.
  • base_url: Refers to the paginated url base.
  • total_rows: Total numer of entries in database.
  • use_page_numbers: Refers whether we want to use page number(1,2,3..) in the url or entry id(1,10, 20…) on uri segment.
Step 3: Create Model
Create a model file named Employee_model.php inside “application/models” folder.
Load “pagination” class in controller.
Step 4: Create controllers
Create a controllers file named Employee.php inside “application/controllers” folder.
  • The Employee controller handles the calendar process.
  • __construct() – Loads the required library, helper and model.
  • index() – load employee data.
  • loadData() – Ajax request and get employee data.
Step 5:Create a view(header)
Create a view file named header.php inside “application/views/templates” folder.
This view contains the header section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header section.
Step 6: Create a view(footer)
Create a view file named footer.php inside “application/views/templates” folder.
This view contains the footer section of the webpage.
Step 7: Create views
Create a views file named index.php inside “application/views/employee” folder.
Step 8: Make Ajax Request and Load Data with Pagination
We will also need to use jQuery code in views index.php in "application/views/employee" directory to load pagination and make Ajax request to load more records.