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:
https://codeigniter.com/user_guide/libra...on#example 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(); Tj
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 $this->load->library('pagination'); // 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) $this->pagination->initialize($config); // 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'; $this->load->library('pagination'); $config['base_url'] = 'http://codeigniter.local/jobs/index'; $config['total_rows'] = 200; $config['per_page'] = 20; $this->pagination->initialize($config); // Where do I put this? echo $this->pagination->create_links(); $this->load->view('templates/header', $data); $this->load->view('jobs/index', $data); $this->load->view('templates/footer'); }
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
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 |
Welcome Guest, Not a member yet? Register Sign In |