0
votes

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 } ?>
2
You forgot $config['uri_segment'] = 3;.Joseph Silber
I tried that at first then saw its easier to pass $offset to the model. If I change the 'function index' to 'function page' and change the base_url to 'test/page' it will also work. Just for some reason it will not work in 'function index'dynamo
pass the $uri_segment to index and check the output.Bhuvan Rikka

2 Answers

2
votes

Try this:

function  __construct()
    {

        parent::__construct();
        $this->load->helper('array');
        $this->load->helper('url');
        $this->load->library('pagination');
        $this->load->model('Test_model','',TRUE);
    }

function index($uri_segment="3")
{   
        $config['base_url'] = base_url('test/index');
        $config['total_rows'] = $this->Test_model->record_count();
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);

$page['data']['products'] = $this->Test_model->getItems($config['per_page'],$this->uri->segment(3));

    $page['data']['pagination']= $this->pagination->create_links();
    $page['title']  = '';
    $page['file']   = 'test/index';

    $this->load->view('template', $page);
}

If the generated listing shows random pages on clicking next page,it should be taking next page number rather than next id(+5). in such case, add

   $config['use_page_numbers'] = TRUE;

before initializing the pagination config.

1
votes

In controller you have to update this

public function index()
    {

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

        $config['base_url'] = base_url().'test/index'; // use test/test and it works
        $config['total_rows'] = $this->Test_model->record_count();
        $config['per_page'] = 5;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;  

        $offset = $this->uri->segment(3,0);

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

        $page['data']['products'] = $this->Test_model->getItems($config['per_page'], $offset);
        $page['data']['pagination'] = $this->pagination->create_links();

        $page['title']  = '';
        $page['file']   = 'test/index';

        $this->load->view('template', $page);
    }

here is helpful link for you Codeigniter pagination