Blog

CodeIgniter Tutorial: How to Create Ajax Pagination using CodeIgniter?

CodeIgniter has many built-in classes and plugins. Pagination class of CodeIgniter is very easy and simple to use. Also Ajax Pagination library is availanle in Codeigniter. Here i will describe how to create simple ajax pagination in Codeigniter using some simple steps.

 

First of all download Ajax Pagination from Codeigniter site. And put this library in “system/libraries/” folder.
Along with library you need to download prototype.js file and put this file any where on root.
Here we have a table which we use for getting records and apply pagination on its rec

 

 

Your controller:

 

[ad#co-1]
Model :

 
PHP
 
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
class FriendsModel extends Model
{
    function FriendsModel()
    {
        parent::Model();
    }
 
    function TotalRec()
    {
      $sql = "SELECT * FROM my_friends";
          $q = $this->db->query($sql);
          return $q->num_rows();
       }
 
    function my_friends($perPage)
    {
        $offset = $this->getOffset()
        $query ="SELECT * FROM  my_friends Order By f_id Desc LIMIT ".$offset.", ".$perPage;
        $q = $this->db->query($query);
        return $q->result();
    }
 
    function getOffset()
    {
        $page = $this->input->post('page');
        if(!$page):
        $offset = 0;
        else:
        $offset = $page;
        endif;
        return $offset;
    }
}
.

Add Comment