Razvan Zamfir Razvan Zamfir - 10 months ago 46
PHP Question

Codeigniter 3: pagination only displays the first 10 records

I am trying to paginate a Codeigniter 3 application without using the framework's pagination library. More exactly, I want to paginate a table with about 100 rows. For this purpose:

In the home controller I have:

public function index() {
$this->load->model('Customer');
$this->load->library('pagination');
$config = [
'base_url' => base_url("/"),
'per_page' => 10,
'total_rows' => $this->Customer->get_num_rows(),
'uri_segment' => 3,
'first_tag_open' => '<li>',
'first_tag_close' => '</li>',
'last_tag_open' => '<li>',
'last_tag_close' => '</li>',
'full_tag_open' => '<ul class="pagination">',
'full_tag_close' => '</ul>',
'next_tag_open' => '<li>',
'next_tag_close' => '</li>',
'prev_tag_open' => '<li>',
'prev_tag_close' => '</li>',
'num_tag_open' => '<li>',
'num_tag_close' => '</li>',
'cur_tag_open' => '<li class="active"><a>',
'cur_tag_close' => '</a></li>',
];
$this->pagination->initialize($config);
$customers = $this->Customer->getCustomers($config['per_page'], $this->uri->segment($config['uri_segment']));
$this->load->view('home', ['records'=>$customers]);
}


In the Model file I have:

class Customer extends CI_Model {
public function __construct() {
$this->load->database();
}

public function getCustomers($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get('customers');
return $query->result();
}
}


Finaly, the view:

<div class="pagination-container text-center">
<?php echo $this->pagination->create_links(); ?>
</div>


The pagination is displayed and properly formatted; so are the first 10 records.
enter image description here

But if I click on page 2, or any other page, I get the Object not found! message from the browser. The url of page 2 is:

http://localhost/cicrud/10


I did something wrong, I can't understand what (I suspect the base_url. In the config file it is
$config['base_url'] = 'http://localhost/cicrud';
). Any help? Thank you!

Answer Source

Add these config lines too...

$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
$config['display_pages'] = TRUE;
$config['use_page_numbers'] = TRUE;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download