Codeigniter Pagination Displaying Too Many Links Codeigniter Pagination Displaying Too Many Links codeigniter codeigniter

Codeigniter Pagination Displaying Too Many Links


This issue is because you need to change the following line of code for each page;

$config['total_rows'] = // number of pages returned here from db

This needs to store the number of pages you wish to display. If you set your pagination configuration within each method then this should be easu enought to do. In your example you are doing this;

$config['total_rows'] = $this->db->get('item')->num_rows();

Presumably you are returning this on each page which is incorrect.


You need to remember two things whenever you are going to implement pagination in Codeigniter:

First thing Configuration related to page generation which is actually:

    // Loads pagination library    $this->load->library('pagination');    // @params $url = your controller + method path    $config['base_url'] = base_url() . $url;    // @params $totalRows = Total  result found in query    $config['total_rows'] = $totalRows;    // @params $perPage = In your case it is 50    $config['per_page'] = $perPage;    // @params $segment = This is what you are missing in your code. Segment is the factor from where system reads which page records need to be shown    $config['uri_segment'] = $segment;    $this->pagination->initialize($config);

Second Thing Pagination configuration related to designing part:

$config['full_tag_open'] = '<ul class="pagination  pagination-sm m-t-none m-b-none">';$config['full_tag_close'] = '</ul>';$config['prev_link'] = '<i class="fa fa-chevron-left"></i>';$config['prev_tag_open'] = '<li>';$config['prev_tag_close'] = '</li>';$config['next_link'] = '<i class="fa fa-chevron-right"></i>';$config['next_tag_open'] = '<li>';$config['next_tag_close'] = '</li>';$config['cur_tag_open'] = '<li class="active"><a href="#">';$config['cur_tag_close'] = '</a></li>';$config['num_tag_open'] = '<li>';$config['num_tag_close'] = '</li>';$config['first_tag_open'] = '<li>';$config['first_tag_close'] = '</li>';$config['last_tag_open'] = '<li>';$config['last_tag_close'] = '</li>';$config['first_link'] = '<i class="fa fa-chevron-left"></i> <i class="fa fa-chevron-left"></i>';$config['last_link'] = '<i class="fa fa-chevron-right"></i><i class="fa fa-chevron-right"></i>';$this->pagination->create_links();

This is the running script which I have been using in my projects. Working fine. You need to check uri_segment you are passing during initialization.

Let me know if you face any issue.


I have the exact same problem: displaying too many link pages. When it should be just showed 10 pages, it displayed 17 pages with the last 7 empty.I follow this answer from the other thread: https://stackoverflow.com/a/21657962. So I basically copy that and paste it to change my previous config.

Here is my previous code that results in error:

$config['full_tag_open'] = '<ul class="pagination">';$config['full_tag_close'] = '</ul>';$config['first_link'] = false;$config['last_link'] = false;$config['first_tag_open'] = '<li>';$config['first_tag_close'] = '</li>';$config['prev_link'] = '«';$config['prev_tag_open'] = '<li class="prev">';$config['prev_tag_close'] = '</li>';$config['next_link'] = '»';$config['next_tag_open'] = '<li>';$config['next_tag_close'] = '</li>';$config['last_tag_open'] = '<li>';$config['last_tag_close'] = '</li>';$config['cur_tag_open'] = '<li class="active"><a href="#">';$config['cur_tag_close'] = '</a></li>';$config['num_tag_open'] = '<li>';$config['num_tag_close'] = '</li>';

delete all that and change it with code from the above code:

$config['full_tag_open'] = "<ul class='pagination'>";$config['full_tag_close'] ="</ul>";$config['num_tag_open'] = '<li>';$config['num_tag_close'] = '</li>';$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";$config['next_tag_open'] = "<li>";$config['next_tagl_close'] = "</li>";$config['prev_tag_open'] = "<li>";$config['prev_tagl_close'] = "</li>";$config['first_tag_open'] = "<li>";$config['first_tagl_close'] = "</li>";$config['last_tag_open'] = "<li>";$config['last_tagl_close'] = "</li>";

then I also happen to have another problem: the result was duplicate, the last page was repeating other data to make a full rows per page.

my previous code:

$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

I changed it to:

$data['page'] = ($this->uri->segment(4)) ? (($this->uri->segment(4) - 1) * $config['per_page']) : 0;

just for the record: my $config['uri_segment'] = 4;