3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = 7 ; // try with 1, 7, 24, 75... $pagination_count = 40 ; // Number of results $maxrecords = 10 ; $num_pages = ceil($pagination_count / $maxrecords) ; // echo "num_pages=$num_pages\n" ; // Just for dev if ($start > $num_pages) { $start = $num_pages; } if ($num_pages < 6) { for ($i = 0; $i < $num_pages ; $i++) { echo render_page_link($i, $start); } } else { // Begining for ($i = 0 ; $i < min(3, $num_pages); $i++) { echo render_page_link($i,$start) ; } echo ' (...) ' ; $have_middle = ($start > 3 && $start <= $num_pages - 3) ; if ($have_middle) { // Around current for ($i = max(3, $start - 3); $i < min($start + 3 - 1, $num_pages - 3) ; $i++) { echo render_page_link($i,$start) ; } } // Ending if ($have_middle) echo ' (...) ' ; for ($i = $num_pages - 3; $i < $num_pages ; $i++) { echo render_page_link($i,$start); } } echo "\n"; // just because I test on CLI. // Here is a little function to display the link: // Currently just "plain text", but could be <a> or <span> with CSS... function render_page_link($index, $current = -1) { if ($index != $current - 1) return ($index+1) . " " ; return "[".($index+1)."] " ; }

preferences:
31.29 ms | 406 KiB | 5 Q