Skip to content

Instantly share code, notes, and snippets.

@farukyildiz
Last active March 12, 2017 12:38
Show Gist options
  • Save farukyildiz/f8707f219e3d20df77c75f44a7ab32ba to your computer and use it in GitHub Desktop.
Save farukyildiz/f8707f219e3d20df77c75f44a7ab32ba to your computer and use it in GitHub Desktop.
Pagination in PHP / function
// LastPageNumber => total pages
// PageIndex => selected page
function Pagination($PageIndex, $LastPageNumber)
{
$ReturnData = "";
if( 1 != (int)$PageIndex )
{
$ReturnData = "<li class='paginate_button previous'>";
$ReturnData = $ReturnData . "<a href='#' aria-controls='datatable-checkbox' data-dt-idx='" . (string)($PageIndex - 1) . "' tabindex='0'>";
$ReturnData = $ReturnData . "Previous";
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
}
$ReturnData = $ReturnData . "<li class='paginate_button'>";
$ReturnData = $ReturnData . "<a href='#'' aria-controls='datatable-checkbox' data-dt-idx='0' tabindex='0'";
$ReturnData = $ReturnData . "<<";
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
if( (int)$PageIndex > 1 ){
$ReturnData = $ReturnData . "<li class='paginate_button'>";
$ReturnData = $ReturnData . "<a href='#'' aria-controls='datatable-checkbox' data-dt-idx='" . (string)($PageIndex - 1) . "' tabindex='0'>";
$ReturnData = $ReturnData . (string)($PageIndex - 1);
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
}
$ReturnData = $ReturnData . "<li class='paginate_button active'>";
$ReturnData = $ReturnData . "<a href='#'' aria-controls='datatable-checkbox' data-dt-idx='" . $PageIndex . "' tabindex='0'>";
$ReturnData = $ReturnData . $PageIndex;
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
if( (int)$LastPageNumber - (int)$PageIndex > 0 ){
$ReturnData = $ReturnData . "<li class='paginate_button'>";
$ReturnData = $ReturnData . "<a href='#'' aria-controls='datatable-checkbox' data-dt-idx='" . (string)($PageIndex + 1) . "' tabindex='0'>";
$ReturnData = $ReturnData . (string)($PageIndex + 1);
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
}
if( (int)$LastPageNumber - (int)$PageIndex > 1 ){
$ReturnData = $ReturnData . "<li class='paginate_button'>";
$ReturnData = $ReturnData . "<a href='#'' aria-controls='datatable-checkbox' data-dt-idx='" . (string)($PageIndex + 2) . "' tabindex='0'>";
$ReturnData = $ReturnData . (string)($PageIndex + 2);
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
}
$ReturnData = $ReturnData . "<li class='paginate_button'>";
$ReturnData = $ReturnData . "<a href='#'' aria-controls='datatable-checkbox' data-dt-idx='" . (string)($PageIndex + 2) . "' tabindex='0'>";
$ReturnData = $ReturnData . ">>";
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
if( (int)$LastPageNumber != (int)$PageIndex )
{
$ReturnData = $ReturnData . "<li class='paginate_button next' id='datatable-checkbox_next'>";
$ReturnData = $ReturnData . "<a href='#' aria-controls='datatable-checkbox' data-dt-idx='1' tabindex='0'>";
$ReturnData = $ReturnData . "Next";
$ReturnData = $ReturnData . "</a>";
$ReturnData = $ReturnData . "</li>";
}
return $ReturnData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment