Brian_15 Posted April 28, 2017 Share Posted April 28, 2017 Need help. This code will generate pagination pages if no result from db. <?php class Pagination { protected $num_rows; protected $current_page; protected $ipp; protected $ipps = array(10, 25, 50, 75, 100); protected $max_links; public function __construct($number_of_rows, $current_page, $items_per_page, int $max_links = 9) { $this->num_rows = $number_of_rows; $this->current_page = $current_page; $this->ipp = $items_per_page; $this->max_links = $max_links; } public function itemsPerPage() { return in_array($this->ipp, $this->ipps) ? $this->ipp : 10; } public function offset() { return ($this->currentPage() - 1) * $this->itemsPerPage(); } public function currentPage() { if ($this->current_page >= $this->totalPages()) { return $this->totalPages(); } elseif ($this->current_page <= 1) { return 1; } else { return $this->current_page; } } public function totalPages() { return ceil($this->num_rows / $this->itemsPerPage()); } /* ----------------- other code ---------------- */ public function pages() { $range = floor(($this->max_links - 1) / 2); $lower_limit = max($this->currentPage() - $range, 1); $upper_limit = min($this->totalPages(), $this->currentPage() + $range); $page_nums = range($lower_limit, $upper_limit); $j = 0; foreach ($page_nums as $i) { $page_links[$j] = $i; $j++; } return $page_links; } } $row_count_from_db = 0; $pagination = new Pagination($row_count_from_db, $_GET['page'] ?? '', $_GET['ipp'] ?? '', 11); var_dump($pagination->pages()); /* output array(2) { [0]=> float(1) [1]=> float(0) } Link to comment Share on other sites More sharing options...
requinix Posted April 28, 2017 Share Posted April 28, 2017 $page_nums = range($lower_limit, $upper_limit);range() supports counting down, such as from 1 to 0. Add some logic in there to make sure that doesn't happen; I suggest returning an empty array if num_rows == 0. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.