Welcome Guest, Not a member yet? Register   Sign In
Using pagination

I'm starting my first project in CodeIgniter and are a little confused by the documentation for using pagination, there is an example on the page:


It gives an example and says it's to use in a controller, I have pasted it in to my contoller but it's not doing anything, also the last line in their example script is:

This can't be right as why would you echo something in the controller? I would understand using echo in a view but definitely not in the controller, could somebody please advise me?

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


You don't need a View, you can do everything inside a Controller and Modal. Or even paste all code in one file.
You can simple copy and paste that into your view.

If you have an error message or similar problem, please provide us with the code you are using.

Thank you for your response,

here is the code example by CodeIgniter: (with my own comments)

// I get this bit, just loading up the required library

// I get this bit, setting some params
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;

// I get this bit, calls the pagination method and passes the array of params over (I assume)

// I don't get where to put this bit, my view doesn't instantiate an instance of the pagination class and if I put it in my controller it would just print it in the top of the page instead of passing it to the view and putting it in it's correct location, this is the bit I'm confused about

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

Currently my controller looks like this:

public function index()
                $data['jobs'] = $this->jobs_model->get_jobs();
                $data['title'] = 'jobs archive';


                $config['base_url'] = 'http://codeigniter.local/jobs/index';
                $config['total_rows'] = 200;
                $config['per_page'] = 20;

                // Where do I put this?
                echo $this->pagination->create_links();

                $this->load->view('templates/header', $data);
                $this->load->view('jobs/index', $data);

Just paste it in your jobs/index view file. Or where you want it.
As long as your controller initiate pagination. Your view file will have access to it.

Ah ok so the view does inherit everything from the controller? Ok I have put the echo $this->pagination->create_links(); into the view and that's working ok, the only thing now is I'm still getting all the results as opposed to iterations of 20?

The click through is broken but I guess that's just me having to learn my routes properly, I'll sort that one out but a little confused about still getting all the results in the first page

(This post was last modified: 03-13-2018, 01:14 PM by jreklund.)

That's correct. You can fecth some things automatic, but some you need to pass along in the $data array.

You need to change your model to accept an offset. It dosen't know you want 20 iterations.

In case you get stuck and want to cheat.
PHP Code:
// Controller
public function page() {
$config['base_url'] = base_url('admin/ads/page');
$config['total_rows'] = $this->db->count_all('ads');
$config['per_page'] = 50;
$config['uri_segment'] = 4// You may need to change this into 3
// You need to change segment(4,1) so it's matching the same number as uri_segment, in your case 3
$offset = (intval($this->uri->segment(4,1))*$config['per_page'])-$config['per_page'];
$this->data['ads'] = $this->ads_model->get_all_ads($config['per_page'],$offset);

// Model
public function get_all_ads($limit,$offset) {
$query $this->db->get('ads',$limit,$offset);

// application/config/pagination.php - Default setttings
('BASEPATH') OR exit('No direct script access allowed');

$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<nav aria-label="Page navigation"><ul class="pagination">';
$config['full_tag_close'] = '</ul></nav>';
$config['prev_link'] = 'Prev.';
$config['next_link'] = 'Next';
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;

$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';

$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li><span>';
$config['cur_tag_close'] = '</span></li>';

$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['num_links'] = 4

Theme © iAndrew 2016 - Forum software by © MyBB