<?php /** * * Get paginated HTML content * @param integer $page current page * @param integer $pages total number of pages * @param string $url The last page number of the jump url address is appended to the url with '&page=x' * * * @return string HTML content; */ function getPageHtml($page, $pages, $url){ // How many page numbers to display at most $_pageNum = 5; //If the current page is less than 1, it is 1 $page = $page<1?1:$page; //The current page is greater than the total number of pages, the total number of pages $page = $page > $pages ? $pages : $page; //The current page is the current page if the number of pages is small $pages = $pages < $page ? $page : $pages; //calculate start page $_start = $page - floor($_pageNum/2); $_start = $_start<1 ? 1 : $_start; //calculate the end page $_end = $page + floor($_pageNum/2); $_end = $_end>$pages? $pages : $_end; //The number of currently displayed page numbers is not enough for the maximum number of pages, and is being adjusted left and right $_curPageNum = $_end-$_start+1; // left adjustment if($_curPageNum<$_pageNum && $_start>1){ $_start = $_start - ($_pageNum-$_curPageNum); $_start = $_start<1 ? 1 : $_start; $_curPageNum = $_end-$_start+1; } // right adjustment if($_curPageNum<$_pageNum && $_end<$pages){ $_end = $_end + ($_pageNum-$_curPageNum); $_end = $_end>$pages? $pages : $_end; } $_pageHtml = '<ul class="pagination">'; /*if($_start == 1){ $_pageHtml .= '<li><a title="第一页">«</a></li>'; }else{ $_pageHtml .= '<li><a title="第一页" href="'.$url.'&page=1">«</a></li>'; }*/ if($page>1){ $_pageHtml .= '<li><a title="上一页" href="'.$url.'&page='.($page-1).'">«</a></li>'; } for ($i = $_start; $i <= $_end; $i++) { if($i == $page){ $_pageHtml .= '<li class="active"><a>'.$i.'</a></li>'; }else{ $_pageHtml .= '<li><a href="'.$url.'&page='.$i.'">'.$i.'</a></li>'; } } /*if($_end == $pages){ $_pageHtml .= '<li><a title="Last page">»</a></li>'; }else{ $_pageHtml .= '<li><a title="最后一页" href="'.$url.'&page='.$pages.'">»</a></li>'; }*/ if($page<$_end){ $_pageHtml .= '<li><a title="下一页" href="'.$url.'&page='.($page+1).'">»</a></li>'; } $_pageHtml .= '</ul>'; echo $_pageHtml; } getPageHtml($_GET['page'], 50, '/page.php?');