Hello. I've beating my head off this pagination script for a couple of hours. Just wondering if anyone can help me make it do something sensible. 20 pages in the bar would great. Right now my first left angle back is broken out of the html, and when on the last page it never outputs back pages. I just get the first, last page buttons and the next page button. <?php class Pagination{ private $url; private $page; private $total_items; private $items_per_page; private $total_pages; private $limit; private $offset; function __construct($itemCount, $item_per_page, $url, $page) { $this->total_items = $itemCount; $this->url = $url; $this->page = (is_numeric($page)? $page : 1); $this->items_per_page = $item_per_page; $this->total_pages = ceil($this->total_items / $this->items_per_page); } public function set_limit($limit) { $this->limit = $limit; } public function set_offset($offset) { $this->offset = $offset; } public function html($appended) { $html = '<div><ul class="pagination"> <li class="page-item"><a class="page-link" href="'.$this->url.'1">«</a></li>'; if($this->page - 1 != 0) { echo '<li class="page-item"><a class="page-link" href="'.$this->url.($this->page - 1).'"><</a></li>'; }else{ echo '<li class="page-item"><a class="page-link" href="'.$this->url.$this->page.'"><</a></li>'; } if($this->total_pages >= 2) { if($this->page <= 20) { if($this->page != $this->total_pages) { for($i = 1; $i <= $this->total_pages; $i++) { $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$i.'">'.$i.'</a></li>'; } }else{ // on the last page, show back to 10 pages for($i = $this->page; $i <= 1; $i--) { $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$i.'">'.$i.'</a></li>'; } } }else { for($i = $this->page; $i <= $this->page + 10; $i++) { $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$i.'">'.$i.'</a></li>'; } } }else{ $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$appended.$this->instance.'=1">1</a></li>'; } if($this->page + 1 < $this->total_pages) { $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.($this->page + 1).'">></a></li>'; }else{ $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$this->page.'">></a></li>'; } $html .= '<li class="page-item"><a class="page-link" href="'.$this->url.$this->total_pages.'">»</a></li>'; $html .= '</ul></div>'; return $html; } function return_pages() { return $this->total_pages; } // End pagination class } ?> Code (markup):
This may or may not help, but I would take a look here: http://www.freezecoders.com/2014/01/simple-pagination-using-php-mysql.html This script I have used a few times in the past an integrated into my PHP and works well. It will of course take a lot of work to integrate, but may be better formed.
Thanks for the reply. Appreciate it, but I'm gonna stick with PDO. Will wait for other replies. Thanks for taking the time, may help another reader
Okay, this class is too confusing. You'd be better off using a simple function Change this line: for($i = $this->page; $i <= 1; $i--) change to: for($i = $this->page; $i >= 1; $i--) Now goto last page and see if you see previous pages link
Maybe going through this CodeIgniter pagination class's code will help you? https://github.com/johnbellone/codeigniter/blob/master/system/libraries/Pagination.php
Normalizing your indentation habits would go a long ways to helping make sense of what you've done. Your setters have zero actual checks for range, meaning there is no reason to use setters or to make those variables private. If you are declaring a bunch of variables in the same scope, comma delimit instead of saying private every joe-blasted time! Are you junk string buffering or not? You're doing echo too -- which is it? What's with the DIV for nothing, endless pointless classes for nothing, and so-forth? Unless you are outputting this twice per page -- and even THEN, I'm REALLY not sure I'd be doing the string addition approach or even making this an object for that matter. It's pagination, that's not rocket science or some big giant hardcore processing. Here, try mine: function template_pagination($urlRoot, $current, $perPage, $total) { if ($total > $perPage) { echo ' <div class="pagination"> Pages: <ul>'; if (($currentPage = floor($current / $perPage)) > 0) echo ' <li><a href="', $urlRoot, '0">First</a></li> <li><a href="', $urlRoot, $currentPage - 1, '" title="previous" rel="previous">«</a></li>'; if (($lastPage = floor(($total - 1) / $perPage)) > 9) { $counter = ($currentPage < 6) ? 0 : $currentPage - 5; if (($endPage = $counter + 10) > $lastPage) { $endPage = $lastPage; $counter = $endPage - 10; } } else { $counter = 0; $endPage = $lastPage; } while ($counter <= $endPage) { echo ' <li>', ( ($noAnchor = ($counter == $currentPage)) ? '<span>' : '<a href="' . $urlRoot . $counter . '">' ), ++$counter, ( $noAnchor ? '</span>' : '</a>' ), '</li>'; } if ($currentPage < $lastPage) echo ' <li><a href="', $urlRoot, ++$currentPage, '" title="next" rel="next">»</a></li> <li><a href="', $urlRoot, $lastPage, '">Last</a></li>'; echo ' </ul> <!-- .pagination --></div>'; } } // template_pagination Code (markup): ... and for the love of Ghu remember, if your parent wrapper has a perfectly good class or ID on it, and EVERY like child element inside it is getting the same classes, NONE of those child elements should be getting classes!!! Oh, and by plugging in rel="next" and rel="prev", the forward/back button in the browser will follow it if there is no history that direction. Handy way to make paginated pages more useful.