<div class="navigation"> <a href="/playlist/1.html?page=6"></a> <a href="/playlist/1.html">1</a> <i>...</i> <a href="/playlist/1.html?page=4">4</a> <a href="/playlist/1.html?page=5">5</a> <a href="/playlist/1.html?page=6">6</a> <span class="link_active">7</span> <a href="/playlist/1.html?page=8">8</a> <a href="/playlist/1.html?page=9">9</a> <a href="/playlist/1.html?page=10">10</a> <i>...</i> <a href="/playlist/1.html?page=17">17</a> <a href="/playlist/1.html?page=8"></a> </div>
1 2 3 4 5 6… 17The first N * 2 pages and the last are displayed.
1 2 3 4 5 6 7 ... 17The first and further formed line is displayed from 4-3 = 1 to 4 + 3 = 7. The first page is reserved so numbers are formed from 2 to 7.
1 2 3 4 5 6 7 8… 17from 5-3 = 2 to 5 + 3 = 8.
1 2 3 4 5 6 7 8 9… 17Perhaps in all the navigation that I saw (including Habr), the line would have been formed with a pass, i.e. 1 ... 3 4 5 6 7 8 9 ... 17
1 ... 4 5 6 7 8 9 10 ... 17The middle is already standard.
1 ... 9 10 11 12 13 14 15 16 17
1 ... 10 11 12 13 14 15 16 17
1 ... 11 12 13 14 15 16 17
1 ... 12 13 14 15 16 17
class PaginateNavigationBuilder { /** * URL * , {page} * : * /some_url{page}.html * : * /some_url.html * /some_url/page_2.html * {page} , * * @var string */ private $baseUrl = '/'; /** * * * @var string */ public $tpl = 'page/{page}/'; /** * * * @var string */ public $wrap = "<div class=\"navigation\">{pages}</div>"; /** * * : * $spread = 2 * 9 5 * 1 ... 3 4 5 6 7 ... 9 * * @var integer */ public $spread = 5; /** * * * @var string */ public $separator = "<i>...</i>"; /** * * * @var string */ public $activeClass = 'link_active'; /** * * * @var integer */ private $currentPage = 0; /** * "" "" * * @var bool */ public $nextPrev = true; /** * "" * * @var string */ public $prevTitle = ''; /** * "" * * @var string */ public $nextTitle = ''; /** * * * @param string $baseUrl URL */ public function __construct($baseUrl = '/') { $this->baseUrl = $baseUrl; } /** * * * @param integer $limit 1 * @param integer $count_all * @param integer $currentPage * @return mixed */ public function build($limit, $count_all, $currentPage = 1) { if( $limit < 1 OR $count_all <= $limit ) return; $count_pages = ceil( $count_all / $limit ); if( $currentPage > $count_pages ) { header( "HTTP/1.0 301 Moved Permanently" ); header( "Location: " . $this->getUrl( $count_pages ) ); die( "Redirect" ); } if( $currentPage == 1 AND $_SERVER['REQUEST_URI'] != $this->getUrl( $currentPage ) ) { header( "HTTP/1.0 301 Moved Permanently" ); header( "Location: " . $this->getUrl( $currentPage ) ); die( "Redirect" ); } $this->currentPage = intval( $currentPage ); if( $this->currentPage < 1 ) $this->currentPage = 1; $shift_start = max( $this->currentPage - $this->spread, 2 ); $shift_end = min( $this->currentPage + $this->spread, $count_pages-1 ); if( $shift_end < $this->spread*2 ) { $shift_end = min( $this->spread*2, $count_pages-1 ); } if( $shift_end == $count_pages - 1 AND $shift_start > 3 ) { $shift_start = max( 3, min( $count_pages - $this->spread*2 + 1, $shift_start ) ); } $list = $this->getItem( 1 ); if ($shift_start == 3) { $list .= $this->getItem( 2 ); } elseif ( $shift_start > 3 ) { $list .= $this->separator; } for( $i = $shift_start; $i <= $shift_end; $i++ ) { $list .= $this->getItem( $i ); } $last_page = $count_pages - 1; if( $shift_end == $last_page-1 ){ $list .= $this->getItem( $last_page ); } elseif( $shift_end < $last_page ) { $list .= $this->separator; } $list .= $this->getItem( $count_pages ); if( $this->nextPrev ) { $list = $this->getItem( $this->currentPage > 1 ? $this->currentPage - 1 : 1, $this->prevTitle, true ) . $list . $this->getItem( $this->currentPage < $count_pages ? $this->currentPage + 1 : $count_pages, $this->nextTitle, true ); } return str_replace( "{pages}", $list, $this->wrap ); } /** * * @param int $page_num * @return string */ private function getUrl( $page_num = 0 ) { $page = $page_num > 1 ? str_replace( '{page}', $page_num, $this->tpl ) : ''; if( stripos( $this->baseUrl, '{page}' ) !== false ){ return str_replace( '{page}', $page, $this->baseUrl ); } else { return $this->baseUrl . $page; } } /** * / * @param int $page_num * @param string $page_name , * @param bool $noclass * @return - span . */ private function getItem( $page_num, $page_name = '', $noclass = false ) { $page_name = $page_name ?: $page_num; $className = $noclass ? '' : $this->activeClass; if( $this->currentPage == $page_num ) { return "<span class=\"{$className}\">{$page_name}</span>"; } else { return "<a href=\"{$this->getUrl($page_num)}\">{$page_name}</a>"; } } }
$navi = new PaginateNavigationBuilder( "/sandbox/" ); $navi->tpl = "page{page}/"; $navi->spread = 4; $template = $navi->build( $limit, $count_all, $page_num );
$navi = new PaginateNavigationBuilder( "/some_url/1{page}.html" ); $navi->tpl = "-page{page}"; $template = $navi->build( $limit, $count_all, $page_num );
Source: https://habr.com/ru/post/340246/
All Articles