CodeIgniter Forums
Easy Pagination Mod for using Page Numbers instead of Offsets - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Easy Pagination Mod for using Page Numbers instead of Offsets (/showthread.php?tid=44676)



Easy Pagination Mod for using Page Numbers instead of Offsets - El Forum - 08-23-2011

[eluser]Unknown[/eluser]
<p>These are instructions on how to make minor edits to the core Pagination.php class so that you can not only use page numbers in URL instead of offsets, but it's also adaptable so that any number of per-page limits can be used.</p>

<p>I've looked far and wide for something like this, but unfortunately I've only found rewritten functions that become more complex and cumbersome to use than the default pagination class. So if you like the default pagination class and simply want to use page numbers instead, this is for you. Here's my documented approach, so you can see how simple and minimal the changes are.</p>

Controller Class

<p>Set up your controller to pass necessary information to both Pagination and View classes. Perform limit, page and offset mathematics in the controller so that they can be passed to other functions/classes.</p>

Code:
// set the per-page entry limit
$iLimit = 10;

// set the current page number
$iPage = $this->uri->segment(3);
$iPage = ($iPage !== false) ? (int)$iPage : 1;

// set the offset by calculating the limit and page number
$iOffset = (($iPage - 1) * $iLimit);

// set pagination configuration
$config['base_url'] = base_url().'entries/page';
$config['first_url'] = base_url().'entries';
$config['uri_segment'] = 3;
$config['per_page'] = $iLimit;

// set total results from db results
$config['total_rows'] = $this->db->get('entries')->num_rows();

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

// load db results using limit and offset variables
$data['query'] = $this->db->get('entries', $iLimit, $iOffset);

// load view
$this->load->view('entries', $data);

Pagination.php Class

<p>Make changes to Pagination class (/system/libraries/Pagination.php) so that it uses page numbers instead of offsets.</p>

OLD (lines 146&ndash;153):

Code:
if ($CI->uri->segment($this->uri_segment) != 0)
{
    $this->cur_page = $CI->uri->segment($this->uri_segment);

    // Prep the current page - no funny business!
    $this->cur_page = (int) $this->cur_page;
}

NEW:

<p>Add 'else' option to if-statement to make sure default is; page = 1.</p>

Code:
if ($CI->uri->segment($this->uri_segment) != 0)
{
    $this->cur_page = $CI->uri->segment($this->uri_segment);

    // Prep the current page - no funny business!
    $this->cur_page = (int) $this->cur_page;
}
else
{
    $this->cur_page = 1;
}


OLD (line 175):

Code:
$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);

NEW:

<p>Simply comment out this line so current page obeys controller/URI.</p>

Code:
//$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);


OLD (line 206):

Code:
$i = $uri_page_number - $this->per_page;

NEW:

<p>Previous page should always be current page subtracted by 1.</p>

Code:
$i = $uri_page_number - 1;


OLD (line 230):

Code:
if ($this->cur_page == $loop)

NEW:

<p>URIs missing pagination should be considered page 1.</p>

Code:
if ($this->cur_page == $loop || ($this->cur_page == 1 && $this->cur_page == $loop))


OLD (line 238&ndash;247):

Code:
if ($n == '' && $this->first_url != '')
{
    $output .= $this->num_tag_open.'&lt;a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

    $output .= $this->num_tag_open.'&lt;a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
}

NEW:

<p>Page URLs should use page numbers and not offsets.</p>

Code:
if ($n == '' && $this->first_url != '')
{
    $output .= $this->num_tag_open.'&lt;a '.$this->anchor_class.'href="'.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

    $output .= $this->num_tag_open.'&lt;a '.$this->anchor_class.'href="'.$this->base_url.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
}


OLD (line 256):

Code:
$output .= $this->next_tag_open.'&lt;a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;

NEW:

<p>Next page should always be the sum of current page and 1.</p>

Code:
$output .= $this->next_tag_open.'&lt;a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page + 1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;


OLD (line 262):

Code:
$i = (($num_pages * $this->per_page) - $this->per_page);

NEW:

<p>Last page should be the total number of pages.</p>

Code:
$i = $num_pages;


Conclusion

<p>That's all you have to do. After these changes, you will be able to use pagination as normal. It's also easy to modify your controller for more advanced queries since all the pagination class requires is the number of results.</p>