Welcome Guest, Not a member yet? Register   Sign In
What's wrong with my pagination?
#1

[eluser]EEssam[/eluser]
Hi,

Here is my code:

Code:
function show()
    {
        // Paginate
        $this->load->library('pagination');

        $page = $this->uri->segment(4, 1);

        $config['base_url'] = site_url('mailinglist/show/');
        $config['total_rows'] = $this->mailinglist_model->get_subscriber_total();
        $config['per_page'] = '2';
        $config['uri_segment'] = '4';

        $this->pagination->initialize($config);

        $data['pagenav'] = $this->pagination->create_links();

        // Counting the offset
        $offset = $page * $config['per_page'];

        // Get subscribers
        $data['mailinglist'] = $this->mailinglist_model->get_subscriber_list($offset, $config['per_page']);

        $this->load->view('mailinglist_show', $data);
    }

Any my model looks like this:

Code:
function get_subscriber_total()
    {
        return $this->db->count_all('mailinglist');;
    }

    function get_subscriber_list($offset, $row_count)
    {
        echo "[$row_count], $offset";
        $this->db->limit($row_count, $offset);
        $query = $this->db->get('mailinglist');

        return $query;
    }

The pagination is showing more pages than what it should show.

Please help.
#2

[eluser]Atas[/eluser]
$page = $this->uri->segment(4); ?
#3

[eluser]EEssam[/eluser]
Huh?
#4

[eluser]Armchair Samurai[/eluser]
I'm totally at a loss as to why you are multiplying your $page and $offset variables. More than likely that is the source of your problems. And unless you're using routing, your uri_segment should be 3.

Also, you don't need the trailing slash for site_url('mailinglist/show')
#5

[eluser]EEssam[/eluser]
Thank you very much Armchair. It's working now, I didn't know CI automatically generate the offset.
#6

[eluser]Chillahan[/eluser]
Whoa - where is CI automatically generating the offset? I echo'ed its config value for per_page before and after ->initialize, no change. (and why would there be? per_page is pretty static.)

This is how I calculate my offset -

if ($this->uri->segment(3))
{
$itemStartIndex = (($this->uri->segment(3) - 1) * $itemsPerPage);
}
else
{
$itemStartIndex = '0';
}

Note that I test segment(3) for existence since if it doesn't exist, it means we're at page 1, so offset should be zero (not 1).

Anyway, I am still having a problem - class pagination will generate a link that says page 3, for example, but when I click it it goes to page 4, which doesn't exist. I am starting to think there is a bug in class pagination for very small page sizes (i.e., two items per page). Anyone else notice this?
#7

[eluser]Chillahan[/eluser]
Never mind, segment 3 IS the index. Actually, I correctly deduced that from the help (somehow) first time around, but then looking through so many examples I got hooked into thinking it was the page number (which is what one would expect it to be, really). So the above should just simply be:

if ($this->uri->segment(3))
{
$itemStartIndex = $this->uri->segment(3);
}
else
{
$itemStartIndex = '0';
}

Sorry about the confusion.




Theme © iAndrew 2016 - Forum software by © MyBB