Skip to content

Instantly share code, notes, and snippets.

@nathanhornby
Created April 14, 2014 15:00
Show Gist options
  • Save nathanhornby/10655929 to your computer and use it in GitHub Desktop.
Save nathanhornby/10655929 to your computer and use it in GitHub Desktop.
CodeIgniter table pagination
<?php
$order_by = ''; // URL segment 2
$sort = 'desc'; // URL segment 3
$page = '1'; // URL segment 4
$data_total = ''; // Total number of entries
$number_of_rows = 10; // Number of table rows per page
$page_limit = 10; // Max pages shown in pagination at a time
if($this->uri->segment(2)){
$order_by = $this->uri->segment(2);
}
if($this->uri->segment(3)){
$sort = $this->uri->segment(3);
}
if($this->uri->segment(4)){
$page = $this->uri->segment(4);
}
$pages_count = ceil( $data_total/$number_of_rows );
if($page > 1){
$prev_page = $page-1;
$prev_disable = '';
}else{
$prev_page = $page;
$prev_disable = 'disable';
}
if(count($feels) == $number_of_rows){
$next_page = $page+1;
$next_disable = '';
}else{
$next_page = $page;
$next_disable = 'disable';
}
?>
<div class="nav_container">
<a class="<?= $prev_disable ?> btn_nav" href="<?= site_url('controller/'.$order_by.'/'.$sort.'/'.$prev_page) ?>">PREV</a>
<?php
if($page >= ceil($page_limit/2)){
$start = $page-ceil($page_limit/2-1);
}else{
$start = 1;
}
if($pages_count < $page_limit){
$pages_displayed = $pages_count;
}else{
$pages_displayed = $page_limit+$start-1;
if($pages_displayed >= $pages_count){
$start = $pages_count-$page_limit+1;
$pages_displayed = $pages_count;
}
}
if($pages_count == $page){
$next_disable = 'disable';
$next_page = $page;
}
if($start > 1){
echo '<a class="btn_nav" href="'.site_url('controller'.$order_by.'/'.$sort.'/'.($start-1)).'">&hellip;</a>';
}
for ($i=$start; $i <= $pages_displayed; $i++) {
echo '<a class="btn_nav '. (($page == $i)? "disable":"") .'" href="'.site_url('controller'.$order_by.'/'.$sort.'/'.$i).'">'.$i.'</a>';
}
if($pages_count > $pages_displayed){
echo '<a class="btn_nav" href="'.site_url('controller/'.$order_by.'/'.$sort.'/'.($pages_displayed+1)).'">&hellip;</a>';
}
?>
<a class="<?= $next_disable ?> btn_nav" href="<?= site_url('controller/'.$order_by.'/'.$sort.'/'.$next_page) ?>">NEXT</a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment