fork download
  1. <?php
  2.  
  3. function paginate1($currentPage, $maxPages, $pagesToShow)
  4. {
  5. $middle = floor($pagesToShow / 2);
  6. $pages = array();
  7.  
  8. $FirstPage = max(1, $currentPage - $middle);
  9. $LastPage = $FirstPage + $pagesToShow - 1;
  10.  
  11. if ($LastPage > $maxPages)
  12. {
  13. $LastPage = $maxPages;
  14. $FirstPage = $LastPage - $pagesToShow + 1;
  15. }
  16.  
  17. for($i = $FirstPage; $i <= $LastPage; $i++)
  18. {
  19. $pages[] = array('number'=> $i, 'active'=>($i==$currentPage));
  20. }
  21. return $pages;
  22. }
  23.  
  24. echo var_dump(paginate1(9,10,5));
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
array(5) {
  [0]=>
  array(2) {
    ["number"]=>
    int(6)
    ["active"]=>
    bool(false)
  }
  [1]=>
  array(2) {
    ["number"]=>
    int(7)
    ["active"]=>
    bool(false)
  }
  [2]=>
  array(2) {
    ["number"]=>
    int(8)
    ["active"]=>
    bool(false)
  }
  [3]=>
  array(2) {
    ["number"]=>
    int(9)
    ["active"]=>
    bool(true)
  }
  [4]=>
  array(2) {
    ["number"]=>
    int(10)
    ["active"]=>
    bool(false)
  }
}