Arrogance
Arrogance

Reputation: 63

CodeIgniter pagination always shows page 1

I already using the pagination library in nearly ten modules, with no problems, but it fails in last one (and the most important).

My routing for this section is:

$route['candidate/sort/(:any)/(:any)/page/(:num)'] = 'candidate/sort/$1/$2/$3';

My Controller

public function sort($type, $id, $page = 1) {
    /* Load Config */
    $data = $this->data;
    $data['sub_active'] = 'candidate';
    $data['type'] = $type;
    /* Get Candidates */
    $total = $this->candidates->getTotal($type, $id);
    if(($this->limit >= $total) && ($page > 1)) {
        $data['candidates'] = $this->candidates->getCandidates(1, $this->limit, $type, $id);
    }elseif(((($this->limit * $page) - $this->limit) >= $total) && ($page > 1)) {
        $data['candidates'] = $this->candidates->getCandidates(ceil($total / $this->limit), $this->limit, $type, $id);
    }else{
        $data['candidates'] = $this->candidates->getCandidates($page, $this->limit, $type, $id);
    }
    /* Pagination */
    $this->load->library('pagination');
    # Config Pagination
    $data['cms']['tables']['total_rows'] = $total;
    $data['cms']['tables']['per_page'] = $this->limit; 
    $data['cms']['tables']['first_url'] = base_url($data['sub_active'].'/sort'.'/'.$type.'/'.$id);
    $data['cms']['tables']['base_url'] = base_url($data['sub_active'].'/sort'.'/'.$type.'/'.$id.'/page');
    $data['page'] = $page;
    $data['total_pages'] = ceil($total / $this->limit);
    $data['total'] = $total;
    # Initialize Pagination
    $this->pagination->initialize($data['cms']['tables']); 
    $data['pagination'] = $this->pagination->create_links();
    /* Display Template */
    $this->twig->display('pages/list_candidate.htm', $data);
}

Base first url = myweb.com/candidate/sort/$type/$id and base url = myweb.com/candidate/sort/$type/$id/page

But the pagination doesn’t work, it always the same page (page 1 on this case). I'm using this same schema in other controllers and it works fine, only fails with this.

Thanks in advance.

Upvotes: 0

Views: 1719

Answers (2)

weia design
weia design

Reputation: 1310

Nice, I am glad you found the answer Tunnecino! But I think its need to be further explained , why

According to our wonderful CI user guide, here is how we determine the uri_segment so not to make the same mistake again :)

$this->uri->segment(n)

Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

http://example.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this:

1.news

2.local

3.metro

4.crime_is_up

By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, this would tell the function to return the number zero in the event of failure:

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

Upvotes: 1

Arrogance
Arrogance

Reputation: 63

I finally found the answer:

$config['uri_segment'] = 6;

That's because codeigniter does not detect well the URL.

Upvotes: 3

Related Questions