一个不错的php分页类代码,可自定义多种分页参数,附有分页调用示例。
1,php分页类
class PageLink {
/* 分页显示参数设置 */
private $db_table = "";
private $db_table_field = "";//要显示的数据库中的字段
private $condition = ""; //查询条件
private $sort = ""; //排序条件
private $page_size = 0; //每页显示的记录数目
private $link_num = 0; //显示页码链接的数目
private $page = 1; //当前页码
private $records = 0; //表中记录总数
private $page_count = 0; //总页数
private $pagestring = ""; //前后分页链接字符串
private $linkUrl = ""; //当前页面路径
private $urlPara = ""; //当前页面url参数
private $linkUrlNum = 1;
/*
* 自定义分页
*/
private $linkFormat = "";
/* 获取的数据 */
private $page_data = "";//从数据库中获取的数据,
//$page_data为二维数组接收
private $dbHelper;
/* 变量定义部分 end */
/* 函数定义(类方法) begin */
function __construct() {include_once 'DBHelper/DBHelper.php';//这是数据库操作类
$this->dbHelper = new DBHelper();
}
/*
* 设置分页信息 begin
* @param $db_table 表
* @param $db_table_field 字段
* @param $condition 條件
* @param $sort 排序
* @param $page_size 顯示條數
* @param $link_num 數字鏈接數
* @param $url 頁面路徑
* @param $para url參數
*/
public function set($db_table, $db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
$this->db_table = $db_table;//表名
$this->db_table_field = $db_table_field; //字段数组,
$this->condition = $condition; //排序条件
$this->sort = $sort; //排序条件
//将要显示的字段名称
//写入该数组
/* db参数设置 end */
/* 分页参数设置 begin */
$this->page_size = $page_size;//每页显示记录的数目
$this->link_num = $link_num;//显示翻页链接的数目
$this->linkUrl = $url;
$this->urlPara = $para;
/* 分页参数设置 end */
}
/* 设置分页信息 end */
/* 获取分页链接数据 begin */
public function get() {
$page_data[0] = $this->pagestring;
$page_data[1] = $this->page_data;
return $page_data;
}
/* 获取分页链接数据 end */
/* 页码处理 begin */
private function set_page() {
if (isset($_REQUEST["page"])) {
$this->page = intval($_REQUEST["page"]);
} else {
$this->page = 1;
}
}
/* 页码处理 end */
/* 获取db中记录的数目 begin */
private function get_records() {
$this->records = $this->dbHelper->counts($this->db_table, $this->condition);
}
/* 获取db中记录的数目 end */
/* 建立翻页链接字符串 begin */
private function page_link() {
$checkPage = intval($this->page / $this->link_num);
$startPage = 1;
$stopPage = 1;
if($checkPage == 0 && $this->page < $this->link_num){
$startPage = 1;
}
$linkPage = ($this->link_num / 2);
if($this->page > $linkPage){
$startPage = $this->page - $linkPage;
}
if(($startPage+$this->link_num)>$this->page_count){
$startPage = $this->page_count - $this->link_num + 1;
}
if($startPage<1){
$startPage = 1;
}
$stopPage = $startPage+($this->link_num - 1);
if($stopPage> $this->page_count){
$stopPage = $this->page_count;
}
$countStr = "共".$this->records."條記錄";
$currStr = "<b>".$this->page."</b>/".$this->page_count."頁 ";
$beginLink = "<a href='".$this->linkUrl."?page=1".$this->urlPara."'>首頁</a>";
$preLink = "<a href='".$this->linkUrl."?page=".($this->page - 1).$this->urlPara."'>上一頁</a>";
$nextLink = "<a href='".$this->linkUrl."?page=".($this->page + 1).$this->urlPara."'>下一頁</a>";
$noPreLink = "<a>上一頁</a>";
$noNextLink = "<a>下一頁</a>";
$endLink = "<a href='".$this->linkUrl."?page=".$this->page_count.$this->urlPara."'>尾頁</a>";
if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
$currPage .= "<a href='".$this->linkUrl."?page=1".$this->urlPara."'>1</a>";
}
for($i=$startPage;$i<=$stopPage;$i++){
if($i == $this->page){
$currPage .= "<span class='current'>".$i."</span>";
}
else{
$currPage .= "<a href='".$this->linkUrl."?page=".$i.$this->urlPara."'>".$i."</a>";
}
}
if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
$currPage .= "<span>...</span><a href='"
.$this->linkUrl."?page=".$this->page_count.$this->urlPara."'>".$this->page_count."</a>";
}
$jumpPage .= "<input type='text' id='page_text_".$this->linkUrlNum.
"' onmouseover='select();' size='1' maxlength='4' onkeyup="this.value=this.value.replace(/D+/g,'');" /> ";
$jumpPage .= "<input type='button' onclick="window.location.href='".$this->linkUrl
."?page='+document.getElementById('page_text_".$this->linkUrlNum."').value+'".$this->urlPara
."'" name='page_submit' value='GO' />";
$this->linkUrlNum++;
if(!empty($this->linkFormat)){
$tempLinkFormat = $this->linkFormat;
$tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
$tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
if($this->page_count > 1){
$tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
if($this->page > 1){
$tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
}
if($this->page < $this->page_count){
$tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
}
$tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
$tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
$tempLinkFormat = str_replace("跳轉", $jumpPage, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("上一頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("下一頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("分頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
}
}
$this->pagestring.=$countStr." ".$currStr;
if($this->page_count > 1){
$this->pagestring.=$beginLink;
if($this->page > 1){
$this->pagestring.=$preLink;
}
$this->pagestring.=$currPage;
if($this->page < $this->page_count){
$this->pagestring.=$nextLink;
}
$this->pagestring.=$endLink.$jumpPage;
}
}
/* 建立翻页链接字符串 end */
/* 获取数据 begin */
private function fetch_data() {
if ($this->records) {
$limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
$this->page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
}
}
/* 获取数据 end */
/* 建立分页 begin */
public function create_page() {
$this->set_page();
$this->get_records();
$this->page_count = ceil($this->records / $this->page_size);
$this->page_link();
$this->fetch_data();
}
/* 建立分页 end */
function __destruct() {
}
/* 函数定义(类方法) end */
}
/*
调用方法
include_once 'PageLink.php';
$pageLink = new PageLink(); //实例化对象
$pageLink->set("table", "*(或字段)", "条件", "排序", "数据条数", "每页链接数", "页面(list.php)", "除?page=1外的其他参数(&id=1&name=test)");//传入参数
$pageLink->create_page();//创建分页
$list = $pageLink->get();//获得数据
echo $list[0];//分页链接
print_r($list[1]); //打印出数据,或按你的需要循环出来
*/