Skip to content

Instantly share code, notes, and snippets.

@yasirtaher
Created April 30, 2016 11:05
Show Gist options
  • Save yasirtaher/9f2b0519c1e275565df16e6d8b2846e4 to your computer and use it in GitHub Desktop.
Save yasirtaher/9f2b0519c1e275565df16e6d8b2846e4 to your computer and use it in GitHub Desktop.
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $yourDataArray ); //total items in array
$limit = 20; //per page
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;
$yourDataArray = array_slice( $yourDataArray, $offset, $limit );
$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';
if( $totalPages != 0 )
{
if( $page == 1 )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> &#171; prev page</a>', $page - 1 );
}
$pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>';
if( $page == $totalPages )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page &#187; </a>', $page + 1 );
}
}
$pagerContainer .= '</div>';
echo $pagerContainer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment