Zoran
Zoran

Reputation: 1371

customized pagination in codeigniter

I have codeigniter applicatin which have pagination code that work like a charm. Pagination allow the visitor to select how much records he would like to see per page. Now, I would like to add the following:

displaying X to Y from total of Z records.

Now, Z is easy...i have that solved. Can anyone tell me how could I achive this?

Here is the function that i have:

  public function displayAllUsers() 
   { 

        if ($this->uri->segment(3) !="") { 
        $limit = $this->uri->segment(3); 
        } else { 
        $limit = 10; 
        } 

        $offset = 4; 
        $offset = $this->uri->segment(4); 
        $this->db->limit($limit, $offset); 
        $data['users'] = $this->backOfficeUsersModel->get();
        $totalresults = $this->db->get('back_office_users')->num_rows();

        $this->session->set_userdata('totalresults', $totalresults);

        //initializing & configuring paging
        $this->load->library('pagination');
        $config['base_url'] = site_url('/backOfficeUsers/displayAllUsers/'.$limit.'/');
        $config['total_rows'] = $totalresults;
        $config['per_page'] = $limit;
        $config['uri_segment'] = 4;
        $config['full_tag_open'] = '<div class="dataTables_paginate paging_bootstrap pagination"><ul>';
        $config['full_tag_close'] = '</ul></div>';
        $config['cur_tag_open'] = '<li><a href=# style="color:#ffffff; background-color:#258BB5;">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config); 

       //....irelevant content.....

    } // end of function displayAllUsers

Any help will be deeply appreciated.

Upvotes: 0

Views: 2792

Answers (1)

Fad
Fad

Reputation: 9858

It shouldn't be that hard. In fact, it's kinda obvious how you can get the value for X and Y if you understand pagination.

First of all, the difference between X and Y will of course be equal to $limit - 1. The value of X should be equal to the variable, $offset, plus 1 (Offset generally starts from 0 not 1). And from there, I'm sure that you understand that Y is equal to $offset + $limit.

$X = $offset + 1;
$Y = $offset + $limit;

In case, you're at the last page, you'll want to make sure you have the right value for Y. You'll need to check the total rows you grab from the database, if it's less than the limit per page, then change it to something like this

if (fetched_rows < $limit)
{
    $Y -= ($limit - fetched_rows); 
}

Upvotes: 1

Related Questions