I edited this after figuring out a few things but is this a good way if I want my links on index? Without the function page, it will not work correctly if the base_url is test/index, but test/test will work.
controller
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Test_model');
$this->load->library('pagination');
}
public function index()
{
$page['title'] = '';
$page['file'] = 'test/index';
$config['base_url'] = base_url().'test/page';
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 2;
$config['num_links'] = 5;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$this->load->view('template', $page);
}
public function page()
{
$page['title'] = '';
$page['file'] = 'test/index';
$config['base_url'] = base_url().'test/page';
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 2;
$config['num_links'] = 5;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$this->load->view('template', $page);
}
}
model
public function record_count()
{
return $this->db->count_all('item');
}
public function getItems($limit, $offset)
{
$query = $this->db->get('item', $limit, $offset);
$result = $query->result();
return $result;
}
view
<h2><?=$pagination; ?></h2>
<table>
<?php foreach($items as $item) { ?>
<tr><td><?=$item->name?></td></tr>
<?php } ?>
$config['uri_segment'] = 3;
. – Joseph Silber$uri_segment
to index and check the output. – Bhuvan Rikka