johnny_s johnny_s - 1 year ago 106
MySQL Question

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download