Home > General, PHP, Tips > CodeIgniter How to Pagination

CodeIgniter How to Pagination

CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks.

CodeIgniter’s Pagination class is very easy to use, and it is 100% customizable, either dynamically or via stored preferences.

If you are not familiar with the term “pagination”, it refers to links that allows you to navigate from page to page, like this:

« First < 1 2 3 4 5 > Last »

function shownews()
{
if($this->userlib->logged_in()) 
//checking for security purposes
{
$this->load->library('pagination');
$per_page = 10; 
// How many pages you want to show in each page
$total = $this->db->get('posts', $per_page, $this->uri->segment(3)); 

//Here is the most important part actually. 
Basically $total variable determined which 
rows you are going to show in the page

$config['base_url'] = base_url().'/index.php/admin/shownews';
$config['total_rows'] = $this->db->count_all('posts'); // Count total rows in the query
$config['per_page'] = $per_page;
$config['num_links'] = 6;
$this->pagination->initialize($config);
$data['posts'] = $total;
$this->load->view('admin_shownews', $data);
} else {
$this->load->view('admin_logineed');
}
}

After getting done with this configuration part of pagination class all you have to do is just initialize it like you are doing it in most of programming languages.

Then by using $this->load->view()  parse your data array to your view which is going to be used for showing paginated items. In that view page you should use this;

<?php echo $this->pagination->create_links(); ?>

to auto create your pagination links. You can put this anwhere in your page you want.

VN:F [1.9.22_1171]
Rating: 7.1/10 (41 votes cast)
VN:F [1.9.22_1171]
Rating: -1 (from 5 votes)
CodeIgniter How to Pagination, 7.1 out of 10 based on 41 ratings
  1. May 20th, 2009 at 02:38 | #1

    Hi,

    Thanks man, your solution worked for me!

    Bye.

    VA:F [1.9.22_1171]
    Rating: 3.3/5 (4 votes cast)
    VA:F [1.9.22_1171]
    Rating: -2 (from 2 votes)
  2. May 6th, 2011 at 10:43 | #2

    How do i do a reverse pagination using this class?

    VA:F [1.9.22_1171]
    Rating: 3.3/5 (4 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 2 votes)
  3. mohnish
    July 10th, 2013 at 08:41 | #3

    hi can you mail me the full code plz..am new to CI……

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  1. No trackbacks yet.