* @version 0.1.0 */ class MyPaginator { /** * Total number of items * @var integer */ protected $TotalItemsCount; /** * Number of items displayed per page * @var integer */ protected $ItemsPerPage; /** * Number of currently displayed page * @var integer */ protected $CurrentPage; /** * Number of Index items in paginator: * example for IndexCount == 10 * Previous | 1 2 3 4 5 6 7 8 ... 18 | Next * 10 items + 2 for Previous and Next item * @var integer */ protected $IndexCount; /** * Total number of pages * @var integer */ protected $TotalPages; protected $Uri; /** * __construct() * @param integer $totalItemsCount * @param integer $itemsPerPage * @param integer $indexCount */ function __construct($totalItemsCount, $itemsPerPage, $indexCount = 10) { $this->CurrentPage = (int) array_key_exists('current_page', $_GET) ? $_GET['current_page'] : 0; $this->CurrentPage = $this->CurrentPage == 0 ? 1 : $this->CurrentPage; $this->TotalItemsCount = $totalItemsCount; $this->ItemsPerPage = $itemsPerPage; $this->TotalPages = ceil($this->TotalItemsCount / $this->ItemsPerPage); $this->IndexCount = $indexCount; $this->IndexCount = $this->IndexCount < $this->TotalPages ? $this->IndexCount : $this->TotalPages; } /** * PrintIndex() is used by {@link Render()} for generating links */ protected function PrintIndex($Page, $Text = null) { $Text = $Text == null ? (int) $Page : $Text; $Page = $Page <= 1 ? null : $Page; $Page = $Page > $this->TotalPages ? $this->TotalPages : $Page; $Page = $Page <= 1 ? null : $Page; $CurrentPage = $this->CurrentPage == 1 ? null : $this->CurrentPage; if($Page != $CurrentPage && $Page != null) { printf('%s ', $this->Uri, $Page, $Text); } else if($Page != $CurrentPage) { printf('%s ', $this->Uri, $Text); } else if(is_integer($Text)) { printf('%s ', $Text); } else { printf('%s ', $Text); } } /** * Render() is used to render paginator (output html) */ public function Render($cssClassName = null) { $cssClassName = $cssClassName == null ? 'paginator' : $cssClassName; printf('
', $cssClassName); $this->Uri = preg_replace('|(.*)\?current_page=\d*$|', '$1', $_SERVER['REQUEST_URI']); /** * First index item */ $this->PrintIndex($this->CurrentPage - 1, 'Previous'); echo ' | '; $this->PrintIndex(1); /** * Second index item */ if($this->IndexCount >= 2) { if(($this->IndexCount >= 5 && $this->IndexCount < $this->TotalPages) && $this->CurrentPage > ceil($this->IndexCount / 2) && $this->CurrentPage != 2) { echo '... '; } else { $this->PrintIndex(2); } } /** * Middle index items */ if($this->IndexCount >= 5) { if($this->CurrentPage <= $this->TotalPages - $this->IndexCount / 2 && $this->CurrentPage > $this->IndexCount / 2) { // we are in middle $start = $this->CurrentPage - ceil(($this->IndexCount - 4) / 2) + 1; $end = $this->CurrentPage + floor(($this->IndexCount - 4) / 2 ); } else if($this->CurrentPage <= $this->IndexCount /2) { // we are in beginning $start = 3; $end = $this->IndexCount - 2; } else { // we are in the end $start = $this->TotalPages - $this->IndexCount + 3; $end = $this->TotalPages - 2; } for($i = $start; $i <= $end; $i++) { $this->PrintIndex($i); } } /** * The last but one index item */ if($this->IndexCount >= 4) { if(($this->IndexCount >= 5 && $this->IndexCount < $this->TotalPages) && $this->CurrentPage < $this->TotalPages - $this->IndexCount / 2) { echo '... '; } else { $this->PrintIndex($this->TotalPages - 1); } } /** * Last index item */ if($this->IndexCount >= 3) { $this->PrintIndex($this->TotalPages); } echo ' | '; $this->PrintIndex($this->CurrentPage + 1, 'Next'); echo '
'; } public function __get($str) { switch($str) { case 'Offset': return $this->ItemsPerPage * ($this->CurrentPage - 1); case 'ItemsPerPage': return $this->ItemsPerPage; case 'CurrentPage': return $this->CurrentPage; case 'TotalPages': return $this->TotalPages; case 'TotalItemsCount': return $this->TotalItemsCount; } } } ?>