jonboy
jonboy

Reputation: 2749

Codeigniter Pagination Displaying Too Many Links

Using Codeigniter 3 and PHP I have built a web application to display results from a MySQL database. All is working as expected as in the pagination successfully allows users to navigate pages, and displays the correct number of results per page.

The issue I have is that there always seems to be too many links on the pagination navigation bar.

For example, I will return 79 records from my database, and display 10 per page. So, there should only be 8 links displaying in the pagination, right? Instead I can see 18 links. The links from 9 to 18 take me to a blank page.

My controller code is below;

public function index() {
    $config['base_url'] = '/items/index';
    $config['use_page_numbers'] = FALSE; 
    $config['reuse_query_string'] = TRUE;
    $config['total_rows'] = $this->db->get('item')->num_rows();
    $config['per_page'] = 10;
    $config['num_links'] = 10;
    $config['full_tag_open'] = '<div><ul class="pagination">';
    $config['full_tag_close'] = '</ul></div><!--pagination-->';
    $config['first_link'] = '&laquo; First';
    $config['first_tag_open'] = '<li class="prev page">';
    $config['first_tag_close'] = '</li>';
    $config['last_link'] = 'Last &raquo;';
    $config['last_tag_open'] = '<li class="next page">';
    $config['last_tag_close'] = '</li>';
    $config['next_link'] = 'Next &rarr;';
    $config['next_tag_open'] = '<li class="next page">';
    $config['next_tag_close'] = '</li>';
    $config['prev_link'] = '&larr; Previous';
    $config['prev_tag_open'] = '<li class="prev page">';
    $config['prev_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';
    $config['anchor_class'] = 'follow_link';
    $this->load->library('pagination');
    $this->pagination->initialize($config);

    $data = array(
    'items' => $this->items_model->itemList()
    );

    $this->load->view('item_list', $data);
}

My view is below;

echo $this->pagination->create_links(); 

My URLs are structured as follows;

items/index    // displays results 1-10
items/index/10 // displays results 11-20
items/index/20 // displays results 21-30
etc...

Any help is appreciated. Thanks

Upvotes: 2

Views: 1812

Answers (5)

Pierre Alexandre
Pierre Alexandre

Reputation: 239

Issue is per_page, total_rows in my case i solved it on :

$page = 0;
$per_page = 8;
$config["total_rows"] = $this->db->get('item')->num_rows();
$config["total_rows"] = $config["total_rows"]/$per_page;
$config["per_page"] = 1;

Upvotes: 0

TheOrdinaryGeek
TheOrdinaryGeek

Reputation: 2323

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.

Upvotes: 3

Emma
Emma

Reputation: 21

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;

Upvotes: 2

Abhishek Singh
Abhishek Singh

Reputation: 509

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.

Upvotes: 2

Trushar Narodia
Trushar Narodia

Reputation: 366

you can try datatable.js first you display all record in table assign unique #id to table for identify after this use jquery and create you simple table with datatable which have all the features like search sorting record navigate page load jquery in footer part load datatable.css and datatable.js in footer part code for jquery

$('#table_id').dataTable();

Upvotes: 0

Related Questions