<?php
/******************************************************
*版权: 高西林
*文件名:class.PageBar.php
*作者:高西林
*日期:2007-07-27
*描述:
生成分页导航栏。
*修改记录:
修改人
日期
记录
高西林 2007-07-30
修正页数为0时,引发的错误!
增加countpgs来统一计算页数。
高西林 2007-07-30 修正风格2,3,4页码后退时看部到
当前页码前一个页码的缺陷。
******************************************************/
class PageBar{
public $pageindex = 1; //当前页码。
public $total = 1; //总记录数。
public $perpage = 10; //每页显示条数。
public $joinword = '?'; //页码导航与原url地址连接词。
public $pageindexword = 'pageindex'; //页码url变量名
public $navigateurl = ''; //原url地址。
public $pagebarstyle = 1; //导航栏的风格。
public $pagenoperpage = 5; //显示页码的数量,只对风格2、3、4起作用。
///
public function __construct(){
}
public function __destruct(){
}
///生成分页导航条
public function createPageBar(){
switch($this -> pagebarstyle){
case 1:
return $this -> style1PageBar();
case 2:
return $this -> style2PageBar();
case 3:
return $this -> style3PageBar();
case 4:
return $this -> style4PageBar();
case 5:
return $this -> style5PageBar();
default:
return $this -> style1PageBar();
}
}
///
/**
生成导航风格1:
显示第1-10条记录 共98条记录
首页 前页 后页 尾页 到第 1 页,共10页
*/
private function style1PageBar(){
$totalpage = $this -> countpgs($this -> total , $this -> perpage);
if($totalpage == 0)return;
//
$beginnum = ($totalpage-1) * $this -> perpage + 1;//本页中第一条记录号
$endnum = $totalpage * $this -> perpage; //本页中最后一条记录号
$navigateprefix = $this -> navigateurl . $this -> joinword . $this -> pageindexword . "=";
$firstpageurl = $navigateprefix."1";
$lastpageurl = $navigateprefix . $totalpage;
$prepageurl = $this -> pageindex > 1 ? "href=/"" . $navigateprefix . ($this -> pageindex -1) . "/"" : '';
$nextpageurl = $this -> pageindex < $totalpage ? "href=/"" . $navigateprefix . ($this -> pageindex +1) . "/"" : '';
//
$navigatebar = "显示第" . $beginnum . "-".$endnum . "条记录
共" . $this -> total . "条记录<br />";
$navigatebar .= "<a href=/"$firstpageurl/">首页</a> ";
$navigatebar .= "<a $prepageurl>前页</a> ";
$navigatebar .= "<a $nextpageurl > 后页</a> ";
$navigatebar .= "<a href=/"$lastpageurl/" >尾页</a> ";
$navigatebar .= "到第<select name=/"select/" οnchange='window.location=/"$url" .
$this -> joinword . $this -> pageindexword . "=/"+this.value'>";
for($i = 1 ; $i <= $totalpage ; $i++){
if($i == $this -> pageindex){
$navigatebar .= "<option value=/"$i/" selected=/"selected/">$i</option>";
}else $navigatebar .= "<option value=/"$i/">$i</option>";
}
$navigatebar .= " </select>页,共10页";
return $navigatebar;
}
/**
生成导航风格1:
< | 1 | 2 | 3 | 4 | 5 | 6 | 7 | >
*/
private function style2PageBar(){
$totalpage = $this -> countpgs($this -> total , $this -> perpage);
if($totalpage == 0)return;
//
$navigateprefix = $this -> navigateurl . $this -> joinword . $this -> pageindexword . "=";
$prepageurl = $this -> pageindex > 1 ? "href=/"" . $navigateprefix . ($this -> pageindex -1) . "/"" : '';
$nextpageurl = $this -> pageindex < $totalpage ? "href=/"" . $navigateprefix . ($this -> pageindex +1) . "/"" : '';
//
$navigatebar .= "<a $prepageurl><</a> | ";
$ii = $this -> startPage($totalpage);
for($i = 0; $i < $this -> pagenoperpage && $i + $ii <= $totalpage ; $i++){
$pageurl = "href=/"" . $navigateprefix . ($i + $ii) . "/"";
if($i + $ii == $this -> pageindex)$pageurl = '';
$navigatebar .= "<a $pageurl>" . ($i + $ii) . "</a> | ";
}
$navigatebar .= "<a $nextpageurl >></a>";
return $navigatebar;
}
/**
生成导航风格1:
上一页 [1] [2] [3] [4] [4] [5] [6] [7] [8] [9] 下一页
*/
private function style3PageBar(){
$totalpage = $this -> countpgs($this -> total , $this -> perpage);
if($totalpage == 0)return;
//
$navigateprefix = $this -> navigateurl . $this -> joinword . $this -> pageindexword . "=";
$prepageurl = $this -> pageindex > 1 ? "href=/"" . $navigateprefix . ($this -> pageindex -1) . "/"" : '';
$nextpageurl = $this -> pageindex < $totalpage ? "href=/"" . $navigateprefix . ($this -> pageindex +1) . "/"" : '';
//
$navigatebar .= "<a $prepageurl>上一页</a> ";
$ii = $this -> startPage($totalpage);
for($i = 0; $i < $this -> pagenoperpage && $i + $ii <= $totalpage ; $i++){
$pageurl = "href=/"" . $navigateprefix . ($i + $ii) . "/"";
if($i + $ii == $this -> pageindex)$pageurl = '';
$navigatebar .= "[<a $pageurl>" . ($i + $ii) . "</a>] ";
}
$navigatebar .= "<a $nextpageurl >下一页</a>";
return $navigatebar;
}
/**
生成导航风格1:
[<<] [<] [1] [2] [3] [4] [5] [6] [>] [>>]
*/
private function style4PageBar(){
$totalpage = $this -> countpgs($this -> total , $this -> perpage);
if($totalpage == 0)return;
//
$navigateprefix = $this -> navigateurl . $this -> joinword . $this -> pageindexword . "=";
$firstpageurl = $navigateprefix."1";
$lastpageurl = $navigateprefix . $totalpage;
$prepageurl = $this -> pageindex > 1 ? "href=/"" . $navigateprefix . ($this -> pageindex -1) . "/"" : '';
$nextpageurl = $this -> pageindex < $totalpage ? "href=/"" . $navigateprefix . ($this -> pageindex +1) . "/"" : '';
//
$navigatebar .= "[<a href=/"$firstpageurl/"><<</a>] ";
$navigatebar .= "[<a $prepageurl><</a>] ";
$ii = $this -> startPage($totalpage);
for($i = 0; $i < $this -> pagenoperpage && $i + $ii <= $totalpage ; $i++){
$pageurl = "href=/"" . $navigateprefix . ($i + $ii) . "/"";
if($i + $ii == $this -> pageindex)$pageurl = '';
$navigatebar .= "[<a $pageurl>" . ($i + $ii) . "</a>] ";
}
$navigatebar .= "[<a $nextpageurl >></a>]";
$navigatebar .= "[<a href=/"$lastpageurl/" >>></a>] ";
return $navigatebar;
}
/**
生成导航风格1:
当前显示第1页, 每页10条|共13条 [首页] [前页] [后页] [尾页] 跳转到 1 页 ,共10页
*/
private function style5PageBar(){
$totalpage = $this -> countpgs($this -> total , $this -> perpage);
if($totalpage == 0)return;
//
$beginnum = ($totalpage-1) * $this -> perpage + 1;//本页中第一条记录号
$endnum = $totalpage * $this -> perpage; //本页中最后一条记录号
$navigateprefix = $this -> navigateurl . $this -> joinword . $this -> pageindexword . "=";
$firstpageurl = $navigateprefix."1";
$lastpageurl = $navigateprefix . $totalpage;
$prepageurl = $this -> pageindex > 1 ? "href=/"" . $navigateprefix . ($this -> pageindex -1) . "/"" : '';
$nextpageurl = $this -> pageindex < $totalpage ? "href=/"" . $navigateprefix . ($this -> pageindex +1) . "/"" : '';
//
$navigatebar = "当前显示第" . $this -> pageindex . "页, 每页" .
$this -> perpage . "条|共" . $this -> total . "条 ";
$navigatebar .= "[<a href=/"$firstpageurl/">首页</a>] ";
$navigatebar .= "[<a $prepageurl>前页</a>] ";
$navigatebar .= "[<a $nextpageurl>后页</a>] ";
$navigatebar .= "[<a href=/"$lastpageurl/" >尾页</a>] ";
$navigatebar .= "跳转到<select name=/"select/" οnchange='window.location=/"$url" .
$this -> joinword . $this -> pageindexword . "=/"+this.value'>";
for($i = 1 ; $i <= $totalpage ; $i++){
if($i == $this -> pageindex){
$navigatebar .= "<option value=/"$i/" selected=/"selected/">$i</option>";
}else $navigatebar .= "<option value=/"$i/">$i</option>";
}
$navigatebar .= " </select>页,共" .$totalpage . "页";
return $navigatebar;
}
//
private function countpgs($total , $perpage){
$this -> perpage = $this -> perpage <= 0 ? 1 : $this -> perpage;
return ceil($this -> total / $this -> perpage);
}
//只对风格2,3,4起作用
private function startPage($totalpage){
$ii = $totalpage - $this -> pageindex;
$ii = $ii+1 >= $this -> pagenoperpage ? $this -> pageindex : $this -> pageindex - ($this ->pagenoperpage - ($ii + 1));
$ii = $ii > 0 ? $ii : 1;
if($ii == $this -> pageindex && $ii > 1)$ii -= 1;
return $ii;
}
}
?>