rondog Posted March 4, 2011 Share Posted March 4, 2011 Hey guys, I am trying to create an ajax pagination class. Right now I have it displaying all the pages. I want to make it so I can display something like 1...8 9 10 11 12 13...28 (when you're in the middle) 1 2 3 4 5 6...28 (when you're in the beginning) 1... 23 24 25 26 27 28 (when you're at the end) Just like its setup on these forums. The number of numbers in the middle, in this case 6, would be defined as well. Can someone point me in the right direction? Here is how I instantiate the class: paginate.php - get's called via ajax <?php if (is_numeric($_REQUEST['page'])) { require_once("paginator.class.php"); $p = new Paginator(); $p->currentPage = $_REQUEST['page']; $p->itemsTotal = 168; // mysql_num_rows value eventually $p->itemsPerPage = 10; // Static number $p->paginate(); ?> <div class="pagination"><?php echo $p->displayPages(); ?></div> <?php } ?> paginator.class.php <?php class Paginator { var $itemsPerPage; var $itemsTotal; var $numPages; var $currentPage; var $output; function Paginator() { } function paginate() { $this->numPages = ceil($this->itemsTotal / $this->itemsPerPage); for ($i = 1; $i <= $this->numPages; $i++) { if ($i == $this->currentPage) { $this->output .= "<span class=\"paginate current\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> "; } else { $this->output .= "<span class=\"paginate\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> "; } } } function displayPages() { return $this->output; } } ?> Any help would be appreciated, thanks! Link to comment https://forums.phpfreaks.com/topic/229624-help-with-pagination-class/ Share on other sites More sharing options...
sasa Posted March 5, 2011 Share Posted March 5, 2011 try <?php class Paginator{ //... function my_p_number($total_pages, $current, $before = 2, $after = 3){ $out = array(1); $out = array_merge($out, range(min($total_pages-$before-$after,max(1, $current - $before)), max($before+$after+1,min($total_pages, $current + $after)))); $out[] = $total_pages; $out = array_unique($out); sort($out); return $out; } function paginate(){ $this->numPages = ceil($this->itemsTotal / $this->itemsPerPage); $p = Paginator::my_p_number($this->numPages, $this->currentPage); $t = 0; foreach ($p as $i){ if($i - $t > 1) $this->output .= ' ... '; if ($i == $this->currentPage){ $this->output .= "<span class=\"paginate current\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> "; }else{ $this->output .= "<span class=\"paginate\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> "; } $t=$i; } } } ?> Link to comment https://forums.phpfreaks.com/topic/229624-help-with-pagination-class/#findComment-1183171 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.