<?php
class Page {
protected $count ;
protected $showPages ;
protected $countPages ;
protected $currPage ;
protected $subPages ;
protected $href ;
protected $page_arr = array ( ) ;
public function __construct ( $count , $showPages , $currPage , $subPages , $href = '' ) {
$this -> count = $count ;
$this -> showPages = $showPages ;
$this -> currPage = $currPage ;
$this -> subPages = $subPages ;
if ( empty ( $href ) ) {
$this -> href = htmlentities ( $_SERVER [ 'PHP_SELF' ] ) ;
} else {
$this -> href = $href ;
}
$this -> construct_Pages ( ) ;
}
public function getPages ( ) {
return $this -> page_arr ;
}
public function showPages ( $style = 1 ) {
$func = 'pageStyle' . $style ;
return $this -> $func ( ) ;
}
protected function pageStyle1 ( ) {
$pageStr = '共' . $this -> count . '条记录,每页显示' . $this -> subPages . '条' ;
$pageStr .= '当前第' . $this -> currPage . '/' . $this -> countPages . '页 ' ;
$_GET [ 'page' ] = 1 ;
$pageStr .= '<span>[<a href="' . $this -> href . '?' . http_build_query ( $_GET ) . '" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>] </span>' ;
if ( $this -> currPage > 1 ) {
$_GET [ 'page' ] = $this -> currPage - 1 ;
$pageStr .= '<span>[<a href="' . $this -> href . '?' . http_build_query ( $_GET ) . '" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上页</a>] </span>' ;
}
foreach ( $this -> page_arr as $k => $v ) {
$_GET [ 'page' ] = $k ;
$pageStr .= '<span>[<a href="' . $v . '" rel="external nofollow" >' . $k . '</a>] </span>' ;
}
if ( $this -> currPage < $this -> countPages ) {
$_GET [ 'page' ] = $this -> currPage + 1 ;
$pageStr .= '<span>[<a href="' . $this -> href . '?' . http_build_query ( $_GET ) . '" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下页</a>] </span>' ;
}
$_GET [ 'page' ] = $this -> countPages ;
$pageStr .= '<span>[<a href="' . $this -> href . '?' . http_build_query ( $_GET ) . '" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾页</a>] </span>' ;
return $pageStr ;
}
protected function construct_Pages ( ) {
$this -> countPages = ceil ( $this -> count / $this -> subPages ) ;
$leftPage_num = floor ( $this -> showPages / 2 ) ;
$rightPage_num = $this -> showPages - $leftPage_num ;
$left = $this -> currPage - $leftPage_num ;
$left = max ( $left , 1 ) ;
$right = $left + $this -> showPages - 1 ;
$right = min ( $right , $this -> countPages ) ;
$left = max ( $right - $this -> showPages + 1 , 1 ) ;
for ( $i = $left ; $i <= $right ; $i ++ ) {
$_GET [ 'page' ] = $i ;
$this -> page_arr [ $i ] = $this -> href . '?' . http_build_query ( $_GET ) ;
}
}
}
? >
使用
< div class = "list-page" >
< ? php
$result_2 = mysqli_query ( $con , "select * from article as a,cate as b where a.cateid=b.id order by a.id desc" ) ;
$result_count = $result_2 -> num_rows ;
$p = new Page ( $result_count , 4 , $page , $subPages ) ;
echo $p -> showPages ( 1 ) ;
? >
< / div>