Pagination 404 on Page 2

#1
[eluser]gscharlemann[/eluser]
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:

Controller:
file located in application/controller/contact/contact.php
Code:
class Contact extends Controller {
    function Contact() {
        parent::Controller();
        $this->load->library('pagination');
        $this->load->model('Contact_model');
    }
  
    function index() {
        $this->_display_contacts();
    }

    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;
        $this->pagination->initialize($config);
        $pagination = $this->pagination->create_links();
        $data['pagination'] = $pagination;
        $this->load->view('contact/display_contacts', $data);
    }
}

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

Page 1 displays all of the results from the database. Page 2 link gets a 404 error (http://www.mysite.com/index.php/contact/contact.html/2) - 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
Code:
site_url("contact/display_contacts");
- 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!

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

#3
[eluser]gscharlemann[/eluser]
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
Code:
$route['(contact)/(\d+)'] = '$1/index/$2'
do? Can you break that down in layman terms?

I'm not good at regular expressions.

#4
[eluser]xwero[/eluser]
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?

#5
[eluser]gscharlemann[/eluser]
After adding your changes, I still get the 404 error and the URL looks like:

http://www.mysite.com/index.php/contact.html/2

The controller is located within a folder labeled contact, so I think the URL should be:
http://www.mysite.com/index.php/contact/contact.html/2

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?

#6
[eluser]gscharlemann[/eluser]
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:
Code:
// 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:
    // mysite.com/index.php/contact/contact/index/#
    // 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;
    
    $this->pagination->initiatlize($config);
    $pagination = $this->pagintation->create_links();
    $data['pagination'] = $pagination;
    $this->load->view('contact/display_contacts', $data);
}

The View (display_contacts.php):
Code:
<table>
&lt;?php foreach($results as $contact):?&gt;
    <tr>
        <td>&lt;?php echo($contact['name'])?&gt;</td>
    </tr>
&lt;?php endforeach;?&gt;
</table>
&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?

#7
[eluser]gscharlemann[/eluser]
scratch that... links are working correctly.

#8
[eluser]unsub[/eluser]
I have a follow up question to this -

I have just put in the routing:
Code:
$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-2019 MyBB Group.