庆宇笔记 - 2015年3月 https://qingyu.me/2015/03/ A Rose by Any Other Name. 一个PHP分页类-支持URL普通模式和PATHINFO模式 https://qingyu.me/archives/%E4%B8%80%E4%B8%AAphp%E5%88%86%E9%A1%B5%E7%B1%BB-%E6%94%AF%E6%8C%81url%E6%99%AE%E9%80%9A%E6%A8%A1%E5%BC%8F%E5%92%8Cpathinfo%E6%A8%A1%E5%BC%8F.html 2015-03-16T16:13:00+08:00 分页实现是网站应用最常用的功能,需要注意的是,要和网站的url模式相一致,且不丢失其他GET参数。再次就是要可以配置你的分页主题。这里我整理了一个分页类,调用非常简单,demo如下:场景:写了一个微信精选的文摘,有分页功能,URL模式就是普通模式"?a=bc&page=2",若你的程序是PATHINTO模式的,实例化Page类时配置一下$urlmode=1就可以生成形如"./a/bc/page/2"的URL。分页类首要知道你的总数和每页的个数就可以了。话不多说,直接看代码加注释应用代码摘取:require './DMysqli.php';//数据库类 $dbconfig = array('hostname'=>SAE_MYSQL_HOST_M,'username'=>SAE_MYSQL_USER,'password'=>SAE_MYSQL_PASS,'database'=>SAE_MYSQL_DB,'hostport'=>SAE_MYSQL_PORT,'tablePrefix'=>'wx_'); $dmysqli = new DMysqli($dbconfig); header('Content-type: text/html; charset=utf-8'); require 'Util/page.class.php';//引入Page分页类 $sql = 'select count(*) from wx_article'; $count = $dmysqli->query($sql);//总数 $totalRow = intval(current($count[0])); $Page = new Page($totalRow,10);//实例化分页类:配置总数、每页个数 $sql = 'select * from wx_article ORDER BY posttime,id desc LIMIT $Page->firstRow'.','.'$Page->listRows';//取出当前页的数据,关键在sql语句的limit语法 $res = $dmysqli->query($sql); /* //分页语种自定义 $Page-&gt;setConfig('header','items'); $Page-&gt;setConfig('prev','Prev Page'); $Page-&gt;setConfig('next','Next Page'); $Page-&gt;setConfig('first','First Page'); $Page-&gt;setConfig('last','Last Page'); $Page-&gt;setConfig('theme',' %totalRow% %header% %nowPage%/%totalPage% Page %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%'); */ $show = $Page-&gt;show();//分页类可以生成分页字符串,然后赋值给变量,在模版里输出就好了! include('template/index.html'); Page类源码如下:class Page { // 分页栏每页显示的页数 public $rollPage = 4; // 页数跳转时要带的参数 public $parameter; // 当url模式为PATHINFO时,此配置项有效 public $depr = '/'; // 分页URL地址 public $url = ''; // 默认列表每页显示行数 public $listRows = 20; // 起始行数 public $firstRow; // 分页总页面数 protected $totalPages; // 总行数 protected $totalRows; // 当前页数 protected $nowPage; // 分页的栏的总页数 protected $coolPages; // 分页显示定制 protected $config = array ( 'header' => '条', 'prev' => '上一页', 'next' => '下一页', 'first' => '第一页', 'last' => '最后一页', 'theme' => ' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%' ); // 默认分页变量名 protected $varPage; // url模式:0 普通模式 1 PATHINFO模式 protected $urlmode; /** * 架构函数 * * @access public * @param array $totalRows * 总的记录数 * @param array $listRows * 每页显示记录数 * @param array $parameter * 分页跳转的参数 * */ public function __construct($totalRows, $listRows = '', $parameter = '', $url = '', $urlmode = 0) { $this->totalRows = $totalRows; $this->parameter = $parameter; $this->varPage = 'page'; if (! empty ( $listRows )) { $this->listRows = intval ( $listRows ); } $this->totalPages = ceil ( $this->totalRows / $this->listRows ); // 总页数 $this->coolPages = ceil ( $this->totalPages / $this->rollPage ); $this->nowPage = ! empty ( $_GET [$this->varPage] ) ? intval ( $_GET [$this->varPage] ) : 1; if (! empty ( $this->totalPages ) && $this->nowPage > $this->totalPages) { $this->nowPage = $this->totalPages; } $this->firstRow = $this->listRows * ($this->nowPage - 1); $this->urlmode = $urlmode; } public function setConfig($name, $value) { if (isset ( $this->config [$name] )) { $this->config [$name] = $value; } } /** * 分页显示输出 * * @access public * */ public function show() { if (0 == $this->totalRows) return ''; $p = $this->varPage; $nowCoolPage = ceil ( $this->nowPage / $this->rollPage ); // 分析分页参数 if ($this->parameter && is_string ( $this->parameter )) { parse_str ( $this->parameter, $parameter ); } elseif (empty ( $this->parameter )) { // unset($_GET[C('VAR_URL_PARAMS')]); if (empty ( $_GET )) { $parameter = array (); } else { $parameter = $_GET; } } $parameter [$p] = '__PAGE__'; if ($this->urlmode == 0) { // 普通模式URL转换 $url = "?" . http_build_query ( $parameter ); } else { // PATHINFO模式或者兼容URL模式 $url = ".$this->depr"; if (! empty ( $parameter )) { // 添加参数 foreach ( $parameter as $var => $val ) { if ('' !== trim ( $val )) $url .= $var . $this->depr . urlencode ( $val ); } } } // 上下翻页字符串 $upRow = $this->nowPage - 1; $downRow = $this->nowPage + 1; if ($upRow > 0) { $upPage = "<a href='" . str_replace ( '__PAGE__', $upRow, $url ) . "'>" . $this->config ['prev'] . "</a>"; } else { $upPage = ''; } if ($downRow <= $this->totalPages) { $downPage = "<a href='" . str_replace ( '__PAGE__', $downRow, $url ) . "'>" . $this->config ['next'] . "</a>"; } else { $downPage = ''; } unset ( $upPage ); unset ( $downPage ); // << < > >> if ($nowCoolPage == 1) { $theFirst = ''; $prePage = ''; } else { $preRow = $this->nowPage - $this->rollPage; $prePage = "<a href='" . str_replace ( '__PAGE__', $preRow, $url ) . "' >上" . $this->rollPage . "页</a>"; $theFirst = "<a href='" . str_replace ( '__PAGE__', 1, $url ) . "' >" . $this->config ['first'] . "</a>"; } if ($nowCoolPage == $this->coolPages) { $nextPage = ''; $theEnd = ''; } else { $nextRow = $this->nowPage + $this->rollPage; $theEndRow = $this->totalPages; $nextPage = "<a href='" . str_replace ( '__PAGE__', $nextRow, $url ) . "' >下" . $this->rollPage . "页</a>"; $theEnd = "<a href='" . str_replace ( '__PAGE__', $theEndRow, $url ) . "' >" . $this->config ['last'] . "</a>"; } // 1 2 3 4 5 $linkPage = ""; for($i = 1; $i <= $this->rollPage; $i ++) { $page = ($nowCoolPage - 1) * $this->rollPage + $i; if ($page != $this->nowPage) { if ($page <= $this->totalPages) { $linkPage .= "&nbsp;<a href='" . str_replace ( '__PAGE__', $page, $url ) . "'>&nbsp;" . $page . "&nbsp;</a>"; } else { break; } } else { if ($this->totalPages != 1) { $linkPage .= "&nbsp;<span class='current'>" . $page . "</span>"; } } } $pageStr = str_replace ( array ( '%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%' ), array ( $this->config ['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd ), $this->config ['theme'] ); return $pageStr; } }