Codeigniter Pagination: In Day 5 Delete Operation we finished all CodeIgniter CRUD operations. Which shows all database records for post table, update existing posts, delete post & create new post. If you haven’t followed CodeIgniter CRUD Course & don’t know what it is or how to implement, I highly recommend you do so. Or if you want, you can download source code files of CodeIgniter CRUD: Day 5 Delete Operation & continue with pagination.
We have a Codeigniter CRUD ready. Which shows list of post on post listing page. Now posts table will become too large with lots of posts. So in near future, It will take much time to load a single page, difficult to search. So we will introduce codeigniter pagination.
Codeigniter Pagination Implementation
In this CodeIgniter Pagination Tutorial, we will use codeigniter’s built-in library. Which has functionality for performing pagination in Codeigniter. So we have to load pagination library first. But before that we have to add our routes capability to handle paginated urls. which will be something like http://localhost:8888/ci/index.php/post/2 . After post we have a number indicating which page to load for post listing.
Step 1: Open application/config/routes.php file and add following code at end of all routes.
1 2 3 |
$route['post/(:num)'] = 'post'; |
Now what this will do? This will handle our every url containing post/<any number> and loads post controller’s index method.
Before working on controller code to implement codeigniter pagination, we have to make some changes to our post model. so that we can use it in controller. Like we need total post count, we only load particular range of posts instead of all posts, etc.
Step 2: Open application/models/postmodel.php file. Add new method countAll to fetch total number of posts in posts table. And also modify getAll method to add limit parameter so that we can fetch limited data from database instead of all data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Postmodel extends CI_Model { var $table = "post"; function __construct() { parent::__construct(); } function getAll($limit = null) { if($limit != null) { $this->db->limit($limit['limit'],$limit['offset']); } $q = $this->db->get($this->table); if($q->num_rows() > 0) { return $q->result(); } return array(); } function countAll() { return $this->db->count_all($this->table); } function add($data) { $this->db->insert($this->table,$data); } function update($data,$id) { $this->db->where("id",$id); $this->db->update($this->table,$data); } function delete($id) { $this->db->where("id",$id); $this->db->delete($this->table); } function getById($id) { $this->db->where("id",$id); $q = $this->db->get($this->table); if($q->num_rows() > 0) { return $q->row(); } return false; } } |
countAll method simply calls active record class’s count_all method to retrieve total number of records count and returns it. getAll method we added a optional argument to adopt new functionality while preserving old one unaffected. Argument containing array with offset & limit key. We pass it to active record class’ limit method which sets limit and offset for query.
Now we are ready to work on our controller to load codeigniter pagination library.
Step 2: Open post controller. and change or replace the index method’s code like below or wherever you want to put pagination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
function index() { $this->load->library('pagination'); $total = $this->post->countAll(); $page = 1; if(@$this->uri->segment(2)) { $page = $this->uri->segment(2); } $limit = 2; $offset = ($page - 1)*$limit; $data['posts'] = $this->post->getAll(array('offset' => $offset,'limit' => $limit)); $config['use_page_numbers'] = TRUE; $config['base_url'] = site_url('post'); $config['total_rows'] = $total; $config['per_page'] = $limit; $config['uri_segment'] = 2; $config['num_links'] = 2; $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); $this->load->view("post/index",$data); } |
First we load codeigniter pagination library. Then we call post model’s newly created method countAll to get total records count. Next thing we needed is current page. Now remember we set our routes in beginning and we have our page information on second segment which is after post in URL. We check for second segment and if it’s available then we set $page variable to that segment. Otherwise default value for $page variable which is 1. Based on current page and total records, we have to calculate offset for fetching posts from database. We passed array containing offset and limit keys to getAll method of post model which we modified in previous step.
Next we create a basic configuration array for pagination containing following keys.
- base_url – setting url without pagination segment.
- use_page_numbers – set it to TRUE for url to store page number instead of starting index of records.
- total_rows – total rows in post table.
- per_page – how many records to display at a time. we set it to 2 so we dont have to enter too many dummy data.
- uri_segment – segment position for current page. for our case it’s 2 because index is default method.
- num_links – specifies how many links to display before and after current page.
After setting all configuration. Pass config array to initialise pagination by calling initialise method of codeigniter pagination library. final step is to call create_links method of codeigniter pagination library to generate pagination links HTML code. We store it in $data['pagination'] and pass it to view along with posts data.
In Next step we will display pagination in view.
Step 3: Open view file located in application/views/post/index.php and add single line of code to display pagination wherever you want it to appear. I am displaying it on top of listing table.
1 2 3 4 5 |
<!-- SOME CODE --> <?=$pagination?> <!-- SOME CODE --> |
Here is what you will get on top of posts listing table.
This is the default look of Codeigniter Pagination Library with few settings changed. We can customise further by tweaking settings.
Customise Codeigniter Pagination Library Appearance
Add following highlighted code in post controller’s index method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
function index() { $this->load->library('pagination'); $total = $this->post->countAll(); $page = 1; if(@$this->uri->segment(2)) { $page = $this->uri->segment(2); } $limit = 2; $offset = ($page - 1)*$limit; $data['posts'] = $this->post->getAll(array('offset' => $offset,'limit' => $limit)); $config['use_page_numbers'] = TRUE; $config['base_url'] = site_url('post'); $config['total_rows'] = $total; $config['per_page'] = $limit; $config['uri_segment'] = 2; $config['num_links'] = 2; $config['full_tag_open'] = "<p style='float:right;'>"; $config['full_tag_close'] = '</p>'; $config['first_link'] = 'First'; $config['last_link'] = 'Last'; $config['next_link'] = '>'; $config['prev_link'] = '<'; $config['cur_tag_open'] = ' <b>'; $config['cur_tag_close'] = '</b>'; $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); $this->load->view("post/index",$data); } |
Open browser and load listing page again and you will get different output similar to below screenshot.
Please like our Facebook Fan page for instant updates on Tutorials.
Pingback: Codeigniter CRUD: Day 5 Delete Operation » CodeRiddles