Simple Pagination Using CodeIgniter and MySQL

Pagination is one of the most frequently used features for web applications. So We have a share in this tutorial, how we can implement CodeIgniter pagination and enhance it further as per our need. Pagination is the process of dividing a document into discrete pages.
Before started to implement the Simple Pagination Using CodeIgniter and MySQL, look files structure:
  • 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 index employee list.
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.