2008年11月8日星期六

PHP 纯粹分页类 超强 超好用 有范例

<?php
class Pager {
private $_perPage; //每一页记录数
private $_totalItems; //总记录数
private $_totalPages; //总共页数
private $_currentPage; // 当前页序号
private $_listStart; //当前头条记录ID
private $_listEnd; //当前最后条记录ID
private $_pageStart; //第一页
private $_pageEnd; //最后页
private $_listPage; //循环显示当前分页列表
private $_listItems; //循环显示当前分页列表条数
private $_pageData; //当前要显示的记录
private $_linkData; //页面传递的变量
private $_startId; //从数据库读出的启始id

public function __construct($totalItems, $perPage, $linkData){
$this->_listItems = 11;
$this->_currentPage = max((int)$_REQUEST['pageId'], 1); //当前的页序号
$this->_perPage = $perPage; //每一页记录数
$this->_totalItems = $totalItems; //总记录数
$this->_totalPages = ceil($totalItems / $perPage); //总页数
$this->_listStart = ($this->_currentPage - 1) * $perPage + 1; //当前头条记录ID
$this->_listEnd = ($this->_currentPage != $this->_totalPages) ? $this->_currentPage * $perPage : $totalItems;//当前最后条记录ID
$this->_startId = $this->_listStart; //从数据库读出的启始id
$this->_linkData = $_SERVER['PHP_SELF'] . $linkData; //传递的变量
}
public function startId(){ //从数据库读出的启始id
return $this->_startId;
}
public function totalItems(){ //总记录数
return $this->_totalItems;
}
public function totalPages(){ //总页数
return $this->_totalPages;
}
public function listStart(){ //当前头条记录ID
return $this->_listStart;
}
public function listEnd(){ //当前最后条记录ID
return $this->_listEnd;
}
public function currentPage(){ //当前页
return $this->_currentPage;
}
public function linkData() { //传递的变量
return $this->_linkData;
}
public function pageStart(){ //第一页
return $this ->_pageStart = '<a href="'.$this->_linkData.'&pageId=1"><FONT title="首页" face="webdings">9</FONT></a>';
}
public function pageEnd(){ //最后页
return $this ->_pageEnd = "<a href=".$this->_linkData."&pageId=".$this->_totalPages."><FONT title=尾页 face=webdings>:</FONT></a>";
}
public function upPage(){ //上一页
$upPage = '';
if($this->_currentPage > 1 && $this->_currentPage <= $this->_totalPages){
$up = $this->_currentPage-1;
$upPage = "<a href=".$this->_linkData."&pageId=".$up."><FONT title=上页 face=webdings>7</FONT></a>";
}
return $upPage;
}
public function downPage(){ //下一页
$downPage = '';
$down = $this->_currentPage+1;
if($this->_currentPage < $this->_totalPages) {
$downPage = "<a href=".$this->_linkData."&pageId=".$down."><FONT title=下页 face=webdings>8</FONT></a>";
}
return $downPage;
}
public function listPage(){ //循环显示当前分页列表
$middlePage = ceil($this->_listItems/2);
if($this->_currentPage <= $middlePage && $this->_currentPage != ""){ //当前页面小于等于中间页,
$start = 1 ;
if($this->_totalPages < $this->_listItems) { $end = $this->_totalPages; }
else {$end = $this->_listItems;}
}elseif(($this->_currentPage + $middlePage) > $this->_totalPages){ // 中间到最后页之间
if($this->_totalPages < $this->_listItems) { $start = 1 ; }
else { $start = $this->_totalPages - $this->_listItems + 1; }
$end = $this->_totalPages;
}else{
$start = $this->_currentPage - $middlePage + 1 ;
$end = $this->_currentPage + $middlePage - 1 ;
}

// 显示列表
for($i = $start; $i <= $end; $i++){
if ($this->_currentPage == $i) { $this->_listPage .= "<strong> ".$i." </strong>&nbsp;"; }
else { $this->_listPage .= "<a href=".$this->_linkData."&pageId=".$i.">".$i."</a>&nbsp;"; }
}
return $this->_listPage;
}
public function jumpPage(){
$jumpPage = '<div><select name="pageId" onchange="location=\''.$this->_linkData.'&pageId=\'+this.value;">'; //页面跳转
for($i = 1; $i <= $this->_totalPages; $i++){
if($i == $this->_currentPage) $selected = ' selected';
$jumpPage .= '<option value="' . $i .'" '.$selected.'>'.$i.'</option>';
unset($selected);
}
return $jumpPage .= '</select> 页</div>';
}
}

///范例
$totalItems = 100; //总记录
$perPage = 7;
$page = new Pager($totalItems, $perPage ,"?a=b&c=6");
//for($i = $page->startId(); $i <= $page->startId() + $perPage; $i++){
for($i = $page->startId(); $i <= $page->listEnd(); $i++){
$data[] = array($i,$i+1);
}
echo "当前开始ID:".$listStart = $page->listStart();echo "<br>";
echo "当前结束ID:".$listEnd = $page->listEnd();echo "<br>";
echo "第一页:".$pageStart = $page->pageStart();echo "<br>";
echo "最后页:".$pageEnd = $page->pageEnd();echo "<br>";
echo "上一页:".$upPage = $page->upPage();echo "<br>";
echo "下一页:".$downPage = $page->downPage();echo "<br>";
echo "列表显示:".$listPage = $page->listPage();echo "<br>";
echo "跳转页:".$jumpPage = $page->jumpPage();echo "<br>";
//echo $page->linkData();
//print_r($data);
echo "<table border=1 width=100><tr><td align=center>N</td><td align=center>N+1</td></tr>";
foreach ($data as $key0 => $value0) {
echo "<tr>";
foreach ($value0 as $key1 => $value1) {
echo "<td align=center>$value1</td>";
}
echo "</tr>";
}
echo "</table>";
?>

没有评论:

博客归档