Last active
June 12, 2020 16:17
-
-
Save D3strukt0r/61d1b7555cb76d5fc6b34635c1b126bf to your computer and use it in GitHub Desktop.
PHP Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$iShowPostAmount = isset($_GET['show']) ? (int)$_GET['show'] : 5; | |
$$iItemLimit = isset($_GET['page']) ? (int)$_GET['page'] : 1; | |
$iTotalItems = (int) getRepliesCount() + 1; | |
$iAdjacents = 1; | |
$iNextPage = $iCurrentPage + 1; | |
$iPreviousPage = $iCurrentPage - 1; | |
$iPagesCount = ceil($iTotalItems / $iItemLimit); | |
$iLastPageM1 = $iPagesCount - 1; | |
if($iPagesCount > 1) | |
{ | |
echo '<ul class="pagination" style="margin:0">'; | |
// Previous page | |
if($iCurrentPage > 1) { | |
echo '<li><a href="/'. '?page=' . $iPreviousPage . '&show=' . $iShowThreadAmount . '" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'; | |
} else { | |
echo '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">«</span></a></li>'; | |
} | |
// Pager | |
if($iPagesCount < 7 + ($iAdjacents * 2)) { //not enough pages to bother breaking it up | |
for($iPageCounter = 1; $iPageCounter <= $iPagesCount; $iPageCounter++) { | |
if($iPageCounter == $iCurrentPage) { | |
echo '<li class="active"><a>' . $iPageCounter . '</a></li>'; | |
} else { | |
echo '<li><a href="/' . '?page=' . $iPageCounter . '&show=' . $iShowThreadAmount . '">' . $iPageCounter . '</a></li>'; | |
} | |
} | |
} | |
elseif($iPagesCount >= 7 + ($iAdjacents * 2)) //enough pages to hide some | |
{ | |
//close to beginning; only hide later pages | |
if($iCurrentPage < 1 + ($iAdjacents * 3)) | |
{ | |
for($iPageCounter = 1; $iPageCounter < 4 + ($iAdjacents * 2); $iPageCounter++) | |
{ | |
if($iPageCounter == $iCurrentPage) | |
echo '<li class="active"><a>' . $iPageCounter . '</a></li>'; | |
else | |
echo '<li><a href="/'. '?page=' . $iPageCounter . '&show=' . $iShowThreadAmount . '">' . $iPageCounter . '</a></li>'; | |
} | |
echo '<li class="disabled"><a>...</a></li>'; | |
echo '<li><a href="/'. '?page=' . $iLastPageM1 . '&show=' . $iShowThreadAmount . '">' . $iLastPageM1 . '</a></li>'; | |
echo '<li><a href="/'. '?page=' . $iPagesCount . '&show=' . $iShowThreadAmount . '">' . $iPagesCount . '</a></li>'; | |
} | |
//in middle; hide some front and some back | |
elseif($iPagesCount - ($iAdjacents * 2) > $iCurrentPage && $iCurrentPage > ($iAdjacents * 2)) | |
{ | |
echo '<li><a href="/'. '?page=1&show=' . $iShowThreadAmount . '">1</a></li>'; | |
echo '<li><a href="/'. '?page=2&show=' . $iShowThreadAmount . '">2</a></li>'; | |
echo '<li class="disabled"><a>...</a></li>'; | |
for($iPageCounter = $iCurrentPage - $iAdjacents; $iPageCounter <= $iCurrentPage + $iAdjacents; $iPageCounter++) | |
{ | |
if($iPageCounter == $iCurrentPage) | |
echo '<li class="active"><a>' . $iPageCounter . '</a></li>'; | |
else | |
echo '<li><a href="/'. '?page=' . $iPageCounter . '&show=' . $iShowThreadAmount . '">' . $iPageCounter . '</a></li>'; | |
} | |
echo '<li class="disabled"><a>...</a></li>'; | |
echo '<li><a href="/'. '?page=' . $iLastPageM1 . '&show=' . $iShowThreadAmount . '">' . $iLastPageM1 . '</a></li>'; | |
echo '<li><a href="/'. '?page=' . $iPagesCount . '&show=' . $iShowThreadAmount . '">' . $iPagesCount . '</a></li>'; | |
} | |
//close to end; only hide early pages | |
else | |
{ | |
echo '<li><a href="/'. '?page=1&show=' . $iShowThreadAmount . '">1</a></li>'; | |
echo '<li><a href="/'. '?page=2&show=' . $iShowThreadAmount . '">2</a></li>'; | |
echo '<li class="disabled"><a>...</a></li>'; | |
for ($iPageCounter = $iPagesCount - (1 + ($iAdjacents * 3)); $iPageCounter <= $iPagesCount; $iPageCounter++) | |
{ | |
if($iPageCounter == $iCurrentPage) | |
echo '<li class="active"><a>' . $iPageCounter . '</a></li>'; | |
else | |
echo '<li><a href="/'. '?page=' . $iPageCounter . '&show=' . $iShowThreadAmount . '">' . $iPageCounter . '</a></li>'; | |
} | |
} | |
} | |
// Next page | |
if($iCurrentPage < $iPageCounter - 1) | |
echo '<li><a href="/'. '?page=' . $iNextPage . '&show=' . $iShowThreadAmount . '" aria-label="Next"><span aria-hidden="true">»</span></a></li>'; | |
else | |
echo '<li class="disabled"><a aria-label="Next"><span aria-hidden="true">»</span></a></li>'; | |
echo '</ul><!-- /.pagination -->'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment