网上找到一个比较好看的分页类,尽管作者已经说明怎么调用了,但是对我来说还是不太明白,主要是在sql语句里面怎么调用,下面是分页类代码:
'100', 需要显示的数据的总条数;
* (必填)'pagesize'=>'2', 每页需要显示的代码数量;
* (必填)'currentpage'=>$_get['p'], 当前页码,默认可以通过$_get['p']获取,其中名字p可以定制
* (必填)'baseurl'=>'/welcome?id=3',你当前页面的链接地址,比如为http://www.xxx.com/test.php(或者/test.php),如果后面带有参数则可以为http://www.xxx.com/test?id=8
* (选填,默认为3)'offset'=>'3', 当前页码的左右偏移量,比如当前页码为5,则在5的左右各显示几个数字链接,默认为3个,则效果为2,3,4,5,6,7,8
* (选填,默认为p)'pagestring'=>'p',通过$_get['p']获取当前页码时候的名字,默认为p
* (选填,默认为here)'classname'=>'here',当前页码链接按钮的样式,默认样式名为here,所以你可以这样写css样式.here{background:#ff4500;} )
*
* 2、可以使用的方法。
* a、初始化类后,需要调用pagination([$style = '1'][,$output=true])方法产生分页链接
* 关于参数的说明:
* @param $style (默认为 1,可不填写) :获取链接全部组件,即 首页+上一页+数字链接+下一页+尾页
* @param $style == 2 :仅获取数字链接
* @param $style == 3 :仅获取上一页+下一页
* @param $style == 4 :仅获取上一页+数字链接+下一页,(不包含首尾页)
*
* @param $output (默认为true),返回分页链接
* @param $output 为false时,直接输出分页链接
*
* b、getcurrentpage()获取当前页码,经过真伪判断后的,防止用户自行输入错误,比如http://www.xxx.com/test?p=-100;此时通过此方法获取当前页码为1
*
* c、pageamount()获取总的页码数量
*
* @author 星空幻颖
* @link http://blog.sina.com.cn/yanyinghq
*
*/
class page
{
private $pagesize; //您的网站每一页显示的列表条数
private $totalrows; //通过数据库查询返回的总的记录条数
private $url; //基准url
private $pageamount; //页码的总数
private $currentpage; //当前的页码
private $offset = 4; //页码偏移量
private $pagestring = 'p'; //页码在url中的名字
private $classhere = 'class=here'; //当前页链接的class样式类名,默认为here
//初始化当前页码,记录总条数,每页多少条记录
public function __construct($param)
{
$this->pagesize = $param['pagesize'];
$this->totalrows = $param['totalrows'];
$this->url = $param['baseurl'];
$this->offset = !empty($param['offset'])?$param['offset']:$this->offset;
$this->pagestring = !empty($param['pagestring'])?$param['pagestring']:$this->pagestring;
$this->classhere = !empty($param['classname'])?$param['classname']:$this->classhere;
$this->currentpage = (int)$param['currentpage'];
}
/**
* 创建分页链接
*
* @param $style 默认为 1 :获取链接全部组件
* @param $style == 2 :仅获取数字链接
* @param $style == 3 :仅获取上一页,下一页
* @param $style == 4 :仅获取上一页、下一页、数字链接,不包含首尾页
*
* @param $output 为true时,返回分页链接
* @param $output 为false时,直接输出分页链接
*
*/
public function pagination($style = '1',$output=true)
{
$this->baseurl();
$this->pageamount();
$this->currentpage();
//获取全部组件
if($style == '1')
{
$page = $this->indexpage().$this->prevpage().$this->pagenumber().$this->nextpage().$this->endpage();
}
else if($style == '2')
{
//获取纯数字链接
$page = $this->pagenumber();
}
else if($style == '3')
{
//只获取上一页下一页
$page = $this->prevpage().$this->nextpage();
}
else if($style =='4')
{
//上一页、下一页、数字链接
$page = $this->prevpage().$this->pagenumber().$this->nextpage();
}
if($output)
{
return $page;
}
else
{
echo $page;
}
}
/**
* 获取当前页码
*
* @return 当前页码,经过真伪判断的
*/
public function getcurrentpage()
{
$this->pageamount();
$this->currentpage();
return $this->currentpage;
}
/**
* 计算出所有的页数
*
* 可以类外面直接调用此方法返回页码总数
*
* @return 页码的总数
*/
public function pageamount()
{
$this->pageamount = ceil( $this->totalrows / $this->pagesize);
if($this->pageamount pageamount = '1';
}
return $this->pageamount;
}
/**
* 判断基准链接是否携带参数
*
* 基准链接为用户提交当前页码链接
*
* 如果携带参数,则在链接之后加&p=
*
* 如果不携带参数,则直接加?p=
*/
private function baseurl()
{
if(preg_match('/\?/', $this->url))
{
$this->url = $this->url.'&'.$this->pagestring.'=';
}
else
{
$this->url = $this->url.'?'.$this->pagestring.'=';
}
}
/**
* 验证当前页码的真伪性
*
* 如果当前页码小于1或者没有,则默认当前页码为1
*
* 如果当前页码大于页码总数,则默认当前页码为页码总数
*
*/
private function currentpage()
{
if($this->currentpage currentpage)
{
$this->currentpage = 1;
}
else if(($this->currentpage > $this->pageamount))
{
$this->currentpage = $this->pageamount;
}
}
/**
* 首页链接
*/
private function indexpage()
{
if($this->currentpage == 1) return;
return 'url.'1>首页';
}
/**
* 尾页链接
*/
private function endpage()
{
if($this->currentpage == $this->pageamount) return;
return 'url.$this->pageamount.'>尾页';
}
/**
* 上一页
*/
private function prevpage()
{
if($this->currentpage == 1) return;
return 'url.( $this->currentpage - 1 ).'>上一页';
}
/**
* 下一页
*/
private function nextpage()
{
if($this->currentpage == $this->pageamount) return;
return 'url.( $this->currentpage + 1 ).'>下一页';
}
/**
* 中间页码的链接
*
*/
private function pagenumber()
{
$left =;
$right = ;
//如果总记录的条数“大于”所有链接的数量时候
if($this->pageamount > ($this->offset * 2 + 1))
{
//当前页码距离首页的距离
$leftnum = $this->currentpage - 1;
//当前页码距离尾页的距离
$rightnum = $this->pageamount - $this->currentpage;
//当当前页码距离首页距离不足偏移量offset时候,在右边补齐缺少的小方块
if( $leftnum offset)
{
//左边的链接
for($i = $leftnum; $i >= 1 ; $i--)
{
$left .= 'url.( $this->currentpage - $i ).'>'.( $this->currentpage - $i ).'';
}
//右边的链接
for($j = 1; $j offset * 2 - $leftnum); $j++)
{
$right .= 'url.( $this->currentpage + $j ).'>'.( $this->currentpage + $j ).'';
}
}
else if($rightnum offset)
{
//左边的链接
for($i = ($this->offset * 2 - $rightnum); $i >= 1 ; $i--)
{
$left .= 'url.( $this->currentpage - $i ).'>'.( $this->currentpage - $i ).'';
}
//右边的链接
for($j = 1; $j currentpage + $j ).'>'.( $this->currentpage + $j ).'';
}
}
else
{
//当前链接左边的链接
for($i = $this->offset; $i >= 1 ; $i--)
{
$left .= 'url.( $this->currentpage - $i ).'>'.( $this->currentpage - $i ).'';
}
//当前链接右边的链接
for($j = 1; $j offset; $j++)
{
$right .= 'url.( $this->currentpage + $j ).'>'.( $this->currentpage + $j ).'';
}
}
return $left.'url.$this->currentpage.' class=here>'.$this->currentpage.''.$right;
}
else
{
$alllink='';
//当页码总数小于需要显示的链接数量时候,则全部显示出来
for($j = 1; $j pageamount; $j++)
{
$alllink.='url.$j.' '.($j == $this->currentpage?$this->classhere:'').'>'.$j.'';
}
return $alllink;
}
}
}
------解决思路----------------------
这个类只负责分页条的产生
唯一可能与数据库查询有关的是参数数组 $param['totalrows'] 项
因为待分页的总行数是查询得到的,所以这个查询应在 $param 赋值之前完成
------解决思路----------------------
楼主也够懒的了,我这里有个给你,样式都写好了
个记录, prev=>上一页, next=>下一页, first=>首 页, last=>尾 页);
private $listnum=8;
/* $total 11. * $listrows 12. */
public function __construct($total, $listrows=10, $pa=){
$this->total=$total;
$this->listrows=$listrows;
$this->uri=$this->geturi($pa);
$this->page=!empty($_get[page]) ? $_get[page] : 1;
$this->pagenum=ceil($this->total/$this->listrows);
$this->limit=$this->setlimit();
}
private function setlimit(){
return limit .($this->page-1)*$this->listrows., {$this->listrows};
}
private function geturi($pa){
$url=$_server[request_uri].(strpos($_server[request_uri], '?')?'':?).$pa;
$parse=parse_url($url);
if(isset($parse[query])){
parse_str($parse['query'],$params);
unset($params[page]);
$url=$parse['path'].'?'.http_build_query($params);
}
return $url;
}
public function __get($args){
if($args==limit)
return $this->limit;
else
return null;
}
private function start(){
if($this->total==0)
return 0;
else
return ($this->page-1)*$this->listrows+1;
}
private function end(){
return min($this->page*$this->listrows,$this->total);
}
private function first(){
if($this->page==1)
$html.='';
else
$html.=uri}&page=1'>{$this->config[first]};
return $html;
}
private function prev(){
if($this->page==1)
$html.='';
else
$html.=uri}&page=.($this->page-1).'>{$this->config[prev]};
return $html;
}
private function pagelist(){
$linkpage=;
$inum=floor($this->listnum/2);
for($i=$inum; $i>=1; $i--){
$page=$this->page-$i;
if($page{$page};
}
$linkpage.={$this->page};
for($i=1; $ipage+$i;
if($pagepagenum)
$linkpage.=uri}&page={$page}'>{$page};
else
break;
}
return $linkpage;
}
private function next(){
if($this->page==$this->pagenum)
$html.='';
else
$html.=uri}&page=.($this->page+1).'>{$this->config[next]};
return $html;
}
private function last(){
if($this->page==$this->pagenum)
$html.='';
else
$html.=uri}&page=.($this->pagenum).'>{$this->config[last]};
return $html;
}
/*private function gopage(){
return ' '.$this->pagenum.')?'.$this->pagenum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'} value='.$this->page.' style=width:25px> '.$this->pagenum.')?'.$this->pagenum.':this.previoussibling.value;location=\''.$this->uri.'&page=\'+page+\'\'> ';
}*/
private function gopage(){
return ' '.$this->pagenum.')?'.$this->pagenum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'} value='.$this->page.' style=width:25px>'.$this->pagenum.')?'.$this->pagenum.':this.previoussibling.value;location=\''.$this->uri.'&page=\'+page+\'\'> ';
}
function fpage($display=array(0,1,2,3,4,5,6,7,8)){
$html[0]=共有{$this->total}{$this->config[header]} ;
$html[1]= 每页显示.($this->end()-$this->start()+1).条,本页{$this->start()}-{$this->end()}条 ;
$html[2]= {$this->page}/{$this->pagenum}页 ;
$html[3]=$this->first();
$html[4]=$this->prev();
$html[5]=$this->pagelist();
$html[6]=$this->next();
$html[7]=$this->last();
$html[8]=$this->gopage();
$fpage='';
foreach($display as $index){
$fpage.=$html[$index];
}
return $fpage;
}
}
?>
具体调用方法:
$num=15; //每页显示数
$page=new page($total,$num);
$result=$db->query(select * from .$db->table('article').$where. order by addtime desc {$page->limit});
循环
fpage(array(3,4,5,6,7,0,1,2,8));?> //php分页类调用,这些数字可以去类文件里面看
fpage(array(3,4,5,6,7,8));?>这是我调用的
css样式文件
.fenye li{float:left; font-family:arial, helvetica, sans-serif; margin-left:6px; display:inline; line-height:30px;}
.fenye a{display:block;height:30px; min-width:30px; text-align:center; font-size:14px; border:1px solid #d6d6d6; float:left; margin-left:3px; padding:3px 5px;line-height:30px;text-decoration:none;color:#666;}
.fenye a:hover{background:#ff4500;border-color:#ff4500; color:#fff;}
.fenye a.here{background:#ff4500;border-color:#ff4500; color:#fff;}
.fenye .sel{background:#e5edf2; color:#333; font-weight:bold; border:1px #c2d5e3 solid; padding:0 12px; border-radius:4px}
下面上效果图:
