最近刚刚完成的一个PHP的分页类,支持两种显示模式,拿出来和各位分享,如有Bug欢迎指正!:)
<?php /** * Page Class * @author Jason Wei <[email protected]> * @version 1.0 */ class Page { private $PageSize; //每页显示行数 private $TotalRow; //总行数 private $ListSize; //页码列表数量 private $PageCount; //总页数 private $DisplayMode; //显示样式 private $PageLink; //链接地址 /** * __construct 构造函数 * * @param int $PageSize 每页显示行数 * @param int $TotalRow 总行数 * @param int $ListSize 页码列表数量 * @param int $DisplayMode 显示样式 * @param string $PageLink 链接地址 * * @example * DisplayMode=1 * 显示样式:共1000条记录,每页显示10条,当前1/100页 [首页] [上页] [下页] [尾页] * DisplayMode=2 * 显示样式:当前1/100页 [首页] [上页] [1][2][3][4][5][6][7][8][9][10] [下页] [尾页] */ function __construct($PageSize, $TotalRow, $ListSize, $DisplayMode, $PageLink) { $this->PageSize = intval($PageSize); $this->TotalRow = intval($TotalRow); $this->ListSize = intval($ListSize); $this->PageCount = ceil($TotalRow / $PageSize); //总页数 = 总行数 / 每页显示行数 if($DisplayMode == 1 || $DisplayMode == 2) { $this->DisplayMode = $DisplayMode; } else { exit('DisplayMode Error'); } $this->PageLink = $PageLink; //exit("$PageSize, $TotalRow, $ListSize, $DisplayMode, $PageLink"); } /** * __destruct 析构函数 */ function __destruct() { unset($PageSize); unset($TotalRow); unset($ListSize); unset($PageCount); unset($PageLink); unset($DisplayMode); } /** * getPage 获取分页 * * @param int $Page 当前页码 * @return string $HtmlCode */ public function getPage($Page = 1) { if($Page == "" || !is_numeric($Page) || $Page < 1) { $Page = 1; } else { $Page = intval($Page); } if($this->DisplayMode == 1) { $HtmlCode = $this->getStyle1($Page); //调用样式1 } elseif($this->DisplayMode == 2) { $HtmlCode = $this->getStyle2($Page); //调用样式2 } return $HtmlCode; } /** * iniArray 初始化页码列表数组 * * @return array */ private function iniArray() { $arrayPage = array(); for($i=0; $i<$this->ListSize; $i++) { $arrayPage[$i] = $i; } return $arrayPage; } /** * constructPageList 构造页码列表 * * @param int $Page 当前页码 * @return array * @example [1][2][3][4][5][6][7][8][9][10] */ private function constructPageList($Page) { if($this->PageCount < $this->ListSize) { $arrayCurrent = array(); for($i=0; $i<$this->PageCount; $i++) { $arrayCurrent[$i] = $i + 1; } } else { $arrayCurrent = $this->iniArray(); $MovePoint = ceil($this->ListSize / 2) + 1; //列表移动点 if($Page <= $MovePoint) { for($i=0; $i<count($arrayCurrent); $i++) { $arrayCurrent[$i] = $i + 1; } } elseif($Page <= $this->PageCount && $Page > $this->PageCount - $this->ListSize + 1 ) { for($i=0; $i<count($arrayCurrent); $i++) { $arrayCurrent[$i] = ($this->PageCount) - ($this->ListSize) + 1 + $i; } } else { for($i=0; $i<count($arrayCurrent); $i++) { $arrayCurrent[$i] = $Page - ($MovePoint - 1) + $i; } } } return $arrayCurrent; } /** * getStyle1 获取显示样式1 * * @param int $Page 当前页码 * @return string */ private function getStyle1($Page) { $HtmlStyle1 = "共".$this->TotalRow."条记录,"; $HtmlStyle1.= "每页显示".$this->PageSize."条,"; $HtmlStyle1.= "当前".$Page."/".$this->PageCount."页 "; if($Page > 1) { $FirstPageURL = $this->PageLink."1"; $PreviewPageURL = $this->PageLink.($Page - 1); $HtmlStyle1 .= "[<a href="$FirstPageURL" mce_href="$FirstPageURL">首页</a>] "; $HtmlStyle1 .= "[<a href="$PreviewPageURL" mce_href="$PreviewPageURL">上一页</a>] "; } else { $HtmlStyle1 .= "[首页] "; $HtmlStyle1 .= "[上一页] "; } if($Page < $this->PageCount) { $LastPageURL = $this->PageLink.$this->PageCount; $NextPageURL = $this->PageLink.($Page + 1); $HtmlStyle1 .= "[<a href="$NextPageURL" mce_href="$NextPageURL">下一页</a>] "; $HtmlStyle1 .= "[<a href="$LastPageURL" mce_href="$LastPageURL">尾页</a>] "; } else { $HtmlStyle1 .= "[下一页] "; $HtmlStyle1 .= "[尾页]"; } return $HtmlStyle1; } /** * getStyle2 获取显示样式2 * * @param int $Page 当前页码 * @return string */ private function getStyle2($Page) { $HtmlStyle2 = "当前".$Page."/".$this->PageCount."页 "; if($Page > 1) { $FirstPageURL = $this->PageLink."1"; $PreviewPageURL = $this->PageLink.($Page - 1); $HtmlStyle2 .= "[<a href="$FirstPageURL" mce_href="$FirstPageURL">首页</a>] "; $HtmlStyle2 .= "[<a href="$PreviewPageURL" mce_href="$PreviewPageURL">上一页</a>] "; } else { $HtmlStyle2 .= "[首页] "; $HtmlStyle2 .= "[上一页] "; } $arrayListSize = $this->constructPageList($Page); for($i=0; $i<count($arrayListSize); $i++) { $NowPage = $arrayListSize[$i]; if($NowPage == $Page) { $HtmlStyle2 .= "[<span style="color:red;" mce_style="color:red;">".$NowPage."</span>]"; } else { $NowPageURL = $this->PageLink.$NowPage; $HtmlStyle2 .= "[<a href="$NowPageURL" mce_href="$NowPageURL">".$NowPage."</a>]"; } } if($Page < $this->PageCount) { $LastPageURL = $this->PageLink.$this->PageCount; $NextPageURL = $this->PageLink.($Page + 1); $HtmlStyle2.= " [<a href="$NextPageURL" mce_href="$NextPageURL">下一页</a>] "; $HtmlStyle2.= "[<a href="$LastPageURL" mce_href="$LastPageURL">尾页</a>] "; } else { $HtmlStyle2 .= "[下一页] "; $HtmlStyle2 .= "[尾页]"; } return $HtmlStyle2; } } ?>
下面是调用方法:
<?php //filename: test.php require_once('Page.class.php'); if(!$_REQUEST['Page']) { $Page = 1; } else { $Page = $_REQUEST['Page']; } $PageSize = 10; $TotalRow = 1000; $ListSize = 10; $DisplayMode = 2; $PageLink = 'test.php?Page='; $objPage = new Page($PageSize, $TotalRow, $ListSize, $DisplayMode, $PageLink); $PageHtml = $objPage->getPage($Page); echo $PageHtml; ?>