php pagination with dots

Solutions on MaxInterview for php pagination with dots by the best coders in the world

showing results for - "php pagination with dots"
Yannik
18 Feb 2020
1<?php 
2  function insertPagination($base_url, $cur_page, $number_of_pages, $prev_next=false) {
3     $ends_count = 1;  //how many items at the ends (before and after [...])
4     $middle_count = 2;  //how many items before and after current page
5     $dots = false;
6     ?>
7     <ul class="pagination">
8     <?php
9     if ($prev_next && $cur_page && 1 < $cur_page) {  //print previous button?
10          ?><li class="prev"><a href="<?php echo $base_url; ?>?page=<?php echo $cur_page-1; ?>">&laquo; Previous</a></li><?php
11     }
12     for ($i = 1; $i <= $number_of_pages; $i++) {
13          if ($i == $cur_page) {
14               ?><li class="active"><a><?php echo $i; ?></a></li><?php
15               $dots = true;
16          } else {
17               if ($i <= $ends_count || ($cur_page && $i >= $cur_page - $middle_count && $i <= $cur_page + $middle_count) || $i > $number_of_pages - $ends_count) { 
18                    ?><li><a href="<?php echo $base_url; ?>?page=<?php echo $i; ?>"><?php echo $i; ?></a></li><?php
19                    $dots = true;
20               } elseif ($dots) {
21                    ?><li><a>&hellip;</a></li><?php
22                    $dots = false;
23               }
24          }
25     }
26     if ($prev_next && $cur_page && ($cur_page < $number_of_pages || -1 == $number_of_pages)) { //print next button?
27          ?><li class="next"><a href="<?php echo $base_url; ?>?page=<?php echo $cur_page+1; ?>">Next &raquo;</a></li><?php
28     }
29     ?>
30     </ul>
31     <?php
32}
33?>