Pagination 404 on Page 2

I'm getting a 404 Page Not Found error when attempting to use pagination. I think it's in the way that I'm sending the base_url to Pagination, but I can't seem to figure out the correct way to specify it. The pagination class is also showing all results from the DB, it's not stopping the results after the "per_page" specification. Here's some code:

file located in application/controller/contact/contact.php
class Contact extends Controller {
    function Contact() {
    function index() {

    function _display_contacts() {
        $query = $this->Contact_model->get_contacts();
        $contact_array = $query->result_array();
        $data['contact_array'] = $contact_array;

        //pagination stuff
        $config['base_url'] = site_url("contact/contact/");
        $config['total_rows'] = $query->num_rows();
        $config['per_page'] = 2;
        $pagination = $this->pagination->create_links();
        $data['pagination'] = $pagination;
        $this->load->view('contact/display_contacts', $data);

file located in: application/views/contact/display_contact.php
&lt;?php foreach($contact_array as $contact):?&gt;
        <td>&lt;?php echo($contact['first_name'])?&gt;</td>
&lt;?php endforeach;?&gt;
&lt;?php echo($pagination)?&gt;

Page 1 displays all of the results from the database. Page 2 link gets a 404 error ( - the config file is setup to put '.html' at the end of the url. My questions are as follows:
1. what URL should I specify as base_url. I've also tried
- but there is no display_contacts() method in the contact controller.
2. what am I doing wrong in the view? how do I get the pagination to work with $contact_array?

Thanks in advance for reading this and providing feedback!

You need routing the way you want it done
$route['(contact)/(\d+)'] = '$1/index/$2';
// controller
$config['base_url'] = site_url("contact");
$config['uri_segment'] = 2;
That should do the trick

Xwero, thanks for the quick reply. I put the first line in the routes.php config file and the second two in the controller. Unfortunately, it didn't work. but I think if I can better understand what you are trying to do I can figure it out. What exactly does the
$route['(contact)/(\d+)'] = '$1/index/$2'
do? Can you break that down in layman terms?

I'm not good at regular expressions.

You want to paginate the index function but because CI fetches the second segment as the controller method you can't make it the pagination segment without routing. (contact) grabs the controller name and (\d+) grabs the digits after the forward slash, $1 and $2 are the placeholders for the grabbed values.

What is wrong after you added my changes?

After adding your changes, I still get the 404 error and the URL looks like:

The controller is located within a folder labeled contact, so I think the URL should be:

I need to learn more about route and uri helpers.

i also realized that all of the values are displaying because I'm not sending the limit and offset to the database query in the model. So if my limit per page is 10 and I get to page 2, my limit will be 20 and offset 10, correct?

Thanks for the help. I've successfully implemented pagination. It's probably not as "clean" as it could be, but I modified the controller method to the following:
// needed to define what page to view.  $index is set to default to
// zero in the index function. i.e.: function index($index = 0) {...
function _display_contacts($index) {
    //because the url looks like so when a new page is requested:
    // i needed to get the 4th uri segment
    if($this->uri->segment(4) != NULL ) {
        $index = $this->uri->segment(4);
    $per_page = 5;
    // i changed the function in the model to accept a limit and offset
    $query = $this->Contact_model->get_contacts($per_page, $index);
    $data['results'] = $query->result_array();

    // set the config file info for pagination.  the library was loaded in the constructor
    $config['base_url'] = site_url("contact/contact/index/");
    $config['total_rows'] = $this->Contact_model->get_total_contact_count();
    $config['per_page'] = $per_page;
    $pagination = $this->pagintation->create_links();
    $data['pagination'] = $pagination;
    $this->load->view('contact/display_contacts', $data);

The View (display_contacts.php):
&lt;?php foreach($results as $contact):?&gt;
        <td>&lt;?php echo($contact['name'])?&gt;</td>
&lt;?php endforeach;?&gt;
&lt;?php echo($pagination)?&gt;

the problem I experience now is when I go to page 2, the correct results are returned, but the pagination links created at the bottom, do not reflect being on page 2. It still says I'm on page 1. Any thoughts as to why? How do I tell the links, which page we are on?

scratch that... links are working correctly.

I have a follow up question to this -

I have just put in the routing:
$route['process/:num'] = "process/browse/:num";
which looks totally different to what you advised, ie: no ( ) and :num instead of \d+.
It's working for me, but now I am curious if that is just fluke, and I should be doing it like what you posted. Can you enlighten me as to what the differences are, and / or any reasons that I should change it?
Yes, it works, but I would rather take the time to make it better if I can.
Thanks very much

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.