0
votes

I am doing a Pagination system in Codeigniter and something is not working properly.

I have a search that gives 5 results in total.

If I put 1 item per page it creates the pagination with the links as

example.com/suppliers/   Page 1
example.com/suppliers/1   Page 2
example.com/suppliers/2   Page 3
example.com/suppliers/3   Page 4
example.com/suppliers/4   Page 5

which is correct

But if I put 2 items per page tt appears as

example.com/suppliers/   Page 1
example.com/suppliers/2   Page 2
example.com/suppliers/4   Page 3

And if I put 3 items per page it appears as

example.com/suppliers/   Page 1
example.com/suppliers/3   Page 2

So it is jumping pages in an order of magnitude equivalent to the results per page.

This is the code used to generate the pagination:

//Create the pagination links
$config['base_url'] = base_url().'/suppliers';
$config['total_rows'] = $this->data['count_suppliers'];
$config['per_page'] = RESULTS_IN_SEARCH;    
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['prev_link'] = '<i class="fa fa-chevron-left"></i>';
$config['next_tag_open'] = '<li class="next">';
$config['next_tag_close'] = '</li>';
$config['next_link'] = '<i class="fa fa-chevron-right"></i>';
$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>';

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

$this->data['pagination']=$this->pagination->create_links();

In the image attached you can see the results.

Pagination problem

SOLVED: Adding $config['use_page_numbers'] = TRUE; to the pagination configuration and when doing the query to the database substract 1 so the Page 1 searches for LIMIT 0, 3 instead of LIMIT 3,3

4
you can try $config['use_page_numbers'] = TRUE;Naumov
I've added your suggestion and then when actually doing the query to the database substract 1 to the page number and it worked. It isn't the most stylish but it works. Thanks. Put it as an answer and I will vote.Marcus
You can edit my answer I'm very bad know English. I understand but can't correct write.Naumov

4 Answers

1
votes

I think the problem is from base_url(). And you can use site_url() instead of base_url(); so please see the difference here.

Base_url - This is the full URL to the controller class/function containing your pagination. In the example above, it is pointing to a controller called “Test” and a function called “page”. Keep in mind that you can re-route your URI if you need a different structure.

$this->load->library('pagination');

$config['base_url'] = base_url().'/suppliers';
$config['total_rows'] = 200;
$config['per_page'] = 20;

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

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

And please read more about pagination here.

0
votes

You can try adding in your array configuration key $config['use_page_numbers'] = TRUE for get numbers page and don't get start numbers item in url.

0
votes

Try to use below code:-

$config['base_url'] = site_url('suppliers/' . $id);

If not working then add below code also:-

$config['uri_segment'] = 4;
$page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$data['topics_array'] = $this->forum->get_topics($id, $config['per_page'], $page);
$data['pagination'] = $this->pagination->create_links();

Refer this link also.

0
votes

you have to set the controller function for page number as 0 if

public function show_entries($page = 0) {          
    ----
    $config['use_page_numbers'] = TRUE;
    $per_page=2; //what ever
    $config["per_page"] = $per_page;
    -----
}