Created
April 3, 2022 23:57
-
-
Save stevesohcot/1681a8bafaf2a8231786553678786b12 to your computer and use it in GitHub Desktop.
PHP pagination
This file contains hidden or 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 function showPagination($arrPagination) { ?> | |
| <?php | |
| $pageLink = $arrPagination['pageLink']; | |
| $urlVarsWithoutPage = $arrPagination['urlVarsWithoutPage']; | |
| $limit = $arrPagination['limit']; | |
| $totalRowCount = $arrPagination['totalRowCount']; | |
| $page = $arrPagination['page']; | |
| // these variables are defined in pagination_setup.php | |
| // $pageLink, $urlVarsWithoutPage, $limit | |
| // $urlVarsWithoutPage will contain the values for $sortBy and $sortOrder | |
| // these variables are defined in the page where the recordset is retrieved | |
| // $totalRowCount | |
| $totalNumPages = ceil($totalRowCount / $limit); | |
| #print "<br>totalRowCount: $totalRowCount"; | |
| #print "<br>limit: $limit"; | |
| #print "<br>totalNumPages: $totalNumPages"; | |
| #print "urlVarsWithoutPage: $urlVarsWithoutPage"; | |
| #print_r($arrPagination); | |
| // Show maximum 5 options | |
| // Back | |
| // Number for page 2 ago | |
| // Number of previous page | |
| // current page | |
| // Number of next page | |
| // Number of page 2 from here | |
| // Next | |
| ?> | |
| <?php if ($totalNumPages > 1) { // only display pagination if applicable ?> | |
| <nav aria-label="Page Navigation"> | |
| <ul class="pagination"> | |
| <?php if ($page > 1) { ?> | |
| <li class="page-item"> | |
| <a class="page-link" href="<?php print $pageLink . '?' . $urlVarsWithoutPage;?>&page=<?php print $page - 1;?>" aria-label="Back">Back</a> | |
| </li> | |
| <?php } else { ?> | |
| <!-- | |
| <li class="page-item disabled"> | |
| <span class="page-link">Back</span> | |
| </li> | |
| --> | |
| <?php } ?> | |
| <?php if ($page - 2 > 0 ) { ?> | |
| <li class="page-item"> | |
| <a class="page-link" href="<?php print $pageLink . '?' . $urlVarsWithoutPage;?>&page=<?php print $page - 2;?>"> | |
| <?php print $page - 2;?> | |
| </a> | |
| </li> | |
| <?php } ?> | |
| <?php if ($page - 1 > 0 ) { ?> | |
| <li class="page-item"> | |
| <a class="page-link" href="<?php print $pageLink . '?' . $urlVarsWithoutPage;?>&page=<?php print $page - 1;?>"> | |
| <?php print $page - 1;?> | |
| </a> | |
| </li> | |
| <?php } ?> | |
| <li class="page-item active" aria-current="page"> | |
| <span class="page-link"> | |
| <?php print $page;?> | |
| <span class="visually-hidden">(current)</span> | |
| </span> | |
| </li> | |
| <?php if ($page + 1 <= $totalNumPages ) { ?> | |
| <li class="page-item"> | |
| <a class="page-link" href="<?php print $pageLink . '?' . $urlVarsWithoutPage;?>&page=<?php print $page + 1;?>"> | |
| <?php print $page + 1;?> | |
| </a> | |
| </li> | |
| <?php } ?> | |
| <?php if ($page + 2 <= $totalNumPages ) { ?> | |
| <li class="page-item"> | |
| <a class="page-link" href="<?php print $pageLink . '?' . $urlVarsWithoutPage;?>&page=<?php print $page + 2;?>"> | |
| <?php print $page + 2;?> | |
| </a> | |
| </li> | |
| <?php } ?> | |
| <?php if ($page != $totalNumPages) { ?> | |
| <li class="page-item"> | |
| <a class="page-link" href="<?php print $pageLink . '?' . $urlVarsWithoutPage;?>&page=<?php print $page + 1;?>" aria-label="Next">Next</a> | |
| </li> | |
| <?php } else { ?> | |
| <!-- | |
| <li class="page-item disabled"> | |
| <span class="page-link">Next</span> | |
| </li> | |
| --> | |
| <?php } ?> | |
| </ul> | |
| </nav> | |
| <?php } // show pagination at all ?> | |
| <?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment