Browse Source

Improve pagination

pull/255/head
Danang Probo Sayekti 10 years ago
parent
commit
3db1004774
1 changed files with 109 additions and 4 deletions
  1. +109
    -4
      system/includes/functions.php

+ 109
- 4
system/includes/functions.php View File

@ -1346,15 +1346,120 @@ function has_pagination($total, $perpage, $page = 1)
if (!$total) { if (!$total) {
$total = count(get_post_unsorted()); $total = count(get_post_unsorted());
} }
$totalPage = ceil($total / $perpage);
$number = 'Page '. $page . ' of ' . $totalPage;
$totalPage = ceil($total / $perpage);
$number = 'Page '. $page . ' of ' . $totalPage;
$pager = get_pagination($page, $total, $perpage, 2);
return array( return array(
'prev' => $page > 1, 'prev' => $page > 1,
'next' => $total > $page * $perpage, 'next' => $total > $page * $perpage,
'number' => $number
'pagenum' => $number,
'html' => $pager,
'items' => $total,
'perpage' => $perpage
); );
} }
//function to return the pagination string
function get_pagination($page = 1, $totalitems, $perpage = 10, $adjacents = 1, $pagestring = '?page=')
{
//defaults
if(!$adjacents) $adjacents = 1;
if(!$perpage) $perpage = 10;
if(!$page) $page = 1;
//other vars
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($totalitems / $perpage); //lastpage is = total items / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = '';
if($lastpage > 1)
{
$pagination .= '<ul class="pagination">';
//previous button
if ($page > 1)
$pagination .= '<li><a href="'. $pagestring . $prev .'">« Prev</a></li>';
else
$pagination .= '<li class="disabled"><span>« Prev</span></li>';
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= '<li class="active"><span>'. $counter.'</span></li>';
else
$pagination .= '<li><a href="'. $pagestring . $counter .'">'. $counter .'</a></li>';
}
}
elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 3))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination .= '<li class="active"><span>'. $counter .'</span></li>';
else
$pagination .= '<li><a href="'. $pagestring . $counter .'">'. $counter .'</a></li>';
}
$pagination .= '<li class="disabled"><span>...</span></li>';
$pagination .= '<li><a href="'. $pagestring . $lpm1 .'">'. $lpm1 .'</a></li>';
$pagination .= '<li><a href="'. $pagestring . $lastpage .'">'. $lastpage .'</a></li>';
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination .= '<li><a href="' . $pagestring .'1">1</a></li>';
$pagination .= '<li><a href="'. $pagestring .'2">2</a></li>';
$pagination .= '<li class="disabled"><span>...</span></li>';
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination .= '<li class="active"><span>'. $counter .'</span></li>';
else
$pagination .= '<li><a href="'. $pagestring . $counter .'">'. $counter .'</a></li>';
}
$pagination .= '<li class="disabled"><span>...</span></li>';
$pagination .= '<li><a href="'. $pagestring . $lpm1 .'">'. $lpm1 .'</a></li>';
$pagination .= '<li><a href="'. $pagestring . $lastpage . '">'. $lastpage .'</a></li>';
}
//close to end; only hide early pages
else
{
$pagination .= '<li><a href="'. $pagestring .'1">1</a></li>';
$pagination .= '<li><a href="'. $pagestring .'2">2</a></li>';
$pagination .= '<li class="disabled"><span>...</span></li>';
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= '<li class="active"><span>'. $counter .'</span></li>';
else
$pagination .= '<li><a href="'. $pagestring . $counter .'">'. $counter .'</a></li>';
}
}
}
//next button
if ($page < $counter - 1)
$pagination .= '<li><a href="'. $pagestring . $next .'">Next »</a></li>';
else
$pagination .= '<li class="disabled"><span>Next »</span></li>';
$pagination .= '</ul>';
}
return $pagination;
}
// Get the meta description // Get the meta description
function get_description($string, $char = null) function get_description($string, $char = null)
{ {
@ -1823,7 +1928,7 @@ function not_found()
'canonical' => site_url(), 'canonical' => site_url(),
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; 404 Not Found', 'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; 404 Not Found',
'bodyclass' => 'error-404', 'bodyclass' => 'error-404',
'is_notfound' => true,
'is_404' => true,
)); ));
die(); die();
} }


Loading…
Cancel
Save