前一段时间写了一个可以自动偏移导航页号的分页显示类,效果有点类似百度的结果页号行("上一页","下一页"之间的一行数字),由于是新手,问题有一
些,现在把它放上来,请各位朋友们帮忙改良一下.如果哪位朋友不嫌弃本人水平有限的话,也可以直接使用,代码我作了简单的测试,还可以.另个哪位朋友知道
在PEAR里有类似的包的话,请把名称发上来,能用PEAR当然就不必再这样麻烦了.
class ShowByPage
{
private $conn;
private $sql;
private $pageSize;
private $groupPages;
private $total;
private $pages;
private $pageNum;
private $startResult;
private $endResult;
private $startPage;
private $endPage;
private $groupStyle;
private $currentPageStyle;
private $GETVarsString;
private $prev;
private $next;
private function setPageNum()
{
if ( isset($_GET["pageNum"]) )
{
$this->pageNum = ceil(0 + $_GET["pageNum"]);
if ( $this->pageNum < 1 || $this->pageNum > $this->pages )
$this->pageNum = 1;
}
else
{
$this->pageNum = 1;
}
}
public function __construct($conn,$sql,$pageSize,$groupPages)
{
$this->conn = $conn;
$this->sql = $sql;
$this->pageSize = $pageSize;
$this->groupPages = $groupPages;
$this->total = mysql_num_rows( mysql_query($sql,$conn) );
$this->pages = ceil( $this->total / $this->pageSize );
$this->setPageNum();
$this->startResult = ( $this->pageNum - 1 ) * $this->pageSize;
$this->endResult = $this->startResult + $this->pageSize - 1;
$this->endResult > $this->total ? $this->endResult = $this->total : true;
if ( $this->pages < $this->groupPages || $this->pageNum <= ceil($this->groupPages/2) )
{
$this->startPage = 1;
}
elseif ( $this->pageNum > $this->pages - ceil($this->groupPages/2) )
{
$this->startPage = $this->pages - $this->groupPages + 1;
}
else
{
$this->startPage = $this->pageNum - ceil( $this->groupPages / 2 ) + 1;
}
$this->endPage = $this->startPage + $this->groupPages - 1;
$this->endPage > $this->pages ? $this->endPage = $this->pages : true;
$this->groupStyle = false;
$this->currentPageStyle = "color:#FF0000;font-size:larger;";
$this->GETVarsString = false;
$this->prev = "前一页";
$this->next = "后一页";
}
public function getTotal()
{
return $this->total;
}
public function getPages()
{
return $this->pages;
}
public function getPageNum()
{
return $this->pageNum;
}
public function getStartResult()
{
return $this->startResult + 1;
}
public function getEndResult()
{
return $this->endResult + 1;
}
public function getResult()
{
return mysql_query( $this->sql." limit ".$this->startResult.",".$this->pageSize, $this->conn );
}
public function setGroupStyle($style)
{
$this->groupStyle = $style;
}
public function setCurrentPageStyle($style)
{
$this->currentPageStyle = $style;
}
public function setGETVarsString($varsString)
{
$this->GETVarsString = $varsString;
}
public function displayGroup()
{
if ( $this->groupStyle )
echo "
groupStyle}\"> ";
else
echo "
";
}
public function setPrevString($prevString)
{
$this->prev = $prevString;
}
public function setNextString($nextString)
{
$this->next = $nextString;
}
public function setPrevImg($src)
{
$this->prev = "
";
}
public function setNextImg($src)
{
$this->next = "
";
}
public function displayPrev()
{
if ( $this->pageNum > 1 )
{
echo "
pageNum-1);
if ( $this->GETVarsString )
echo "&{$this->GETVarsString}";
echo "\">{$this->prev}";
}
}
public function displayNext()
{
if ( $this->pageNum < $this->pages )
{
echo "
pageNum+1);
if ( $this->GETVarsString )
echo "&{$this->GETVarsString}";
echo "\">{$this->next}";
}
}
}
?>
//ShowByPage类说明文档
//
//__construct( $conn, $sql, $pageSize, $groupPages ) 构造函数
//$conn 数据库有效连接
//$sql 可以得到所有结果(不分页)的SQL语句
//$pageSize 每页最多显示结果数目
//$groupPages 用来导航的页编号数目
//getTotal() 返回所有结果的总数
//getPages() 返回结果的最大页数
//getPageNum() 返回当前所在页的结果页数
//getStartResult() 返回当前页第一条结果的编号(从1开始)
//getEndResult() 返回当前页最后一条结果的编号
//getResult() 返回当前页的数据库结果集(就是mysql查询返回的结果集)
//setGroupStyle( $style ) 设置用来导航的结果页号的css样式(当前所在页的页号例外)
//$style 导航的结果页号样式(html标记的style属性值)的样式
//displayGroup() 显示导航页号列表
//setCurrentPageStyle( $style ) 设置导航页号列表中的当前所在页的css样式
//$style 导航页号列表中的当前所在页的样式( html标记的style属性值)
//setGETVarsString( $varsString ) 设置应在各页导航时传递的附加GET变量字符串
//$varsString 各页导航时传递的GET变量字符串( html的a标记href属性值内?之后的变量名和值组成的字符串)
//setPrevString( $prevString ) 设置导航到前一页的字符串(不应与setPreImg()一起使用)
//$prevString 导航到前一页的字符串标识
//setNextString( $nextString ) 设置导航到后一页的字符串
//$nextString 导航到后一页的字符串标识
//setPrevImg( $src ) 设置导航到前一页的图片URL(不应与setPrevString()一起使用)
//$src 导航到前一页的图片的URL值
//setNextImg( $src ) 设置导航到后一面的图片URL(不应与setNextString()一起使用)
//$src 导航到后一页的图片的URL值
//displayPrev() 显示导航到前一页的链接(当前为第一页时不显示)
//displayNext() 显示导航到后一页的链接(当前为最后一页时不显示)
?>
我个人的感觉是本来应该分为几个类,不过当时图个方便,就全放一个类里了,还有就是没有显示查询结果函数,这主要是考虑到使用模板.
--------------------next---------------------
阅读(511) | 评论(0) | 转发(0) |