Created
April 15, 2020 22:22
-
-
Save jrfoell/bc9cab9deedd4ee181e4106a259af2dc to your computer and use it in GitHub Desktop.
Specific Pagination Example
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
| #!/usr/bin/env php | |
| <?php | |
| $page = 9; | |
| $max = 16; | |
| $pages = range( 1, $max ); | |
| echo "Current page: {$page}\n"; | |
| // Case 1 (7 or less pages) | |
| if ( count( $pages ) <= 7 ) { | |
| print_r( $pages ); | |
| exit; | |
| } | |
| $new_pages = []; | |
| if ( $page <= 5 ) { // Case 2 (Current page less than 5) | |
| echo "Beginning\n"; | |
| $new_pages = array_slice( $pages, 0, 5 ); | |
| $new_pages[] = '...'; | |
| $new_pages[] = array_pop( $pages ); | |
| $pages = $new_pages; | |
| } elseif ( $page > 5 && $page < ( $max - 3 ) ) { // Case 3 (Current page > 5 but less than $max - 3) | |
| echo "Middle\n"; | |
| $new_pages[] = $pages[0]; | |
| $new_pages[] = '...'; | |
| $new_pages = array_merge( $new_pages, array_slice( $pages, $page - 2, 3 ) ); | |
| if ( $max - $page <= 3 ) { // Case 3a. | |
| $new_pages[] = $pages[ count( $pages ) - 2 ]; | |
| } else { // Case 3b. | |
| $new_pages[] = '...'; | |
| } | |
| $new_pages[] = array_pop( $pages ); | |
| $pages = $new_pages; | |
| } else { // Case 4 | |
| echo "End\n"; | |
| $new_pages[] = $pages[0]; | |
| $new_pages[] = '...'; | |
| $new_pages = array_merge( $new_pages, array_slice( $pages, -5 ) ); | |
| $pages = $new_pages; | |
| } | |
| print_r( $pages ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment