最新消息:

PHP翻页类 调用实例

PHP PHPaihaozhe 71浏览 0评论

PHP翻页类 调用实例

<?php

/*

1.当前翻页的Url问题(方法)

2.获取翻页位置的名称

3.获取当前翻页位置的值

4.设置每页所显示的数量

5.获取总记录数

6.如果显示数字索引时,设置所显示的索引范围数量

7.设置首页的名称

8.设置上一页的名称

9.设置下一页的名称

10.设置尾页的名称

11.设置当前翻页位置的样式

*/

class Page

{

private $CurrentPageValue;//获取当前翻页位置的值

private $AllListCount;//获取总记录数

private $EachPageCount=’10’;//设置每页所显示的数量

private $PageName=’page’;//获取翻页位置的名称

private $IndexShowCount=’5′;//如果显示数字索引时,设置所显示的索引范围数量

private $HomeName=’首页’;//设置首页的名称

private $PrevName=’上一页’;//设置上一页的名称

private $NextName=’下一页’;//设置下一页的名称

private $EndName=’末页’;//设置尾页的名称

private $CurrentSeatClass=’current’;//设置当前翻页位置的样式

private $CurrentUrl;//获取当前的Url

private $AllPageCount;//获取总页数

//依据需要重新给自定义属性赋值

public function __construct($Config=array())

{

if(!empty($Config[‘CurrentPageValue’])){

$Config[‘CurrentPageValue’]=abs($Config[‘CurrentPageValue’]);

$this->CurrentPageValue=$Config[‘CurrentPageValue’];

}else{

$this->CurrentPageValue=1;

}

if(!empty($Config[‘EachPageCount’])){$this->EachPageCount=$Config[‘EachPageCount’];}

if(!empty($Config[‘AllListCount’])){$this->AllListCount=$Config[‘AllListCount’];}

if(!empty($Config[‘PageName’])){$this->PageName=$Config[‘PageName’];}

if(!empty($Config[‘IndexShowCount’])){$this->IndexShowCount=$Config[‘IndexShowCount’];}

if(!empty($Config[‘HomeName’])){$this->HomeName=$Config[‘HomeName’];}

if(!empty($Config[‘PrevName’])){$this->PrevName=$Config[‘PrevName’];}

if(!empty($Config[‘NextName’])){$this->NextName=$Config[‘NextName’];}

if(!empty($Config[‘EndName’])){$this->EndName=$Config[‘EndName’];}

if(!empty($Config[‘CurrentSeatClass’])){$this->CurrentSeatClass=$Config[‘CurrentSeatClass’];}

$this->ResetUrl();//重新设置当前的Url

$this->GetAllPageCount();

}

//外部调用的公共入口

public function CallPage($ShowType=’H,P,L,N,E’)

{

$Str='’;

if(!empty($ShowType)&&!empty($this->AllListCount))

{

$ShowTypeArr=explode(‘,’,$ShowType);

if(in_array(‘H’,$ShowTypeArr)){

$Str.=$this->SetHomePage();

}

if(in_array(‘P’,$ShowTypeArr)){

$Str.=$this->SetPrevName();

}

if(in_array(‘L’,$ShowTypeArr)){

$Str.=$this->SetLink();

}

if(in_array(‘N’,$ShowTypeArr)){

$Str.=$this->SetNextName();

}

if(in_array(‘E’,$ShowTypeArr)){

$Str.=$this->GetEndName();

}

}

return $Str;

}

public function BackLimit()

{

return ($this->CurrentPageValue-1)*$this->EachPageCount.’,’.$this->EachPageCount;

}

//获取总页数

public function GetAllPageCount()

{

$AllPage=ceil($this->AllListCount/$this->EachPageCount);

$this->AllPageCount=empty($AllPage)?1:$AllPage;

return $this->AllPageCount;

}

//重新设置当前的Url

private function ResetUrl()

{

//print_r($_SERVER);

$Url=$_SERVER[‘REQUEST_SCHEME’].’://’.$_SERVER[‘HTTP_HOST’];

$CurrentUrl=$Url.$_SERVER[‘REQUEST_URI’];

$UrlArr=parse_url($CurrentUrl);

$ParamArr=array();

if(!empty($UrlArr[‘query’]))

{

parse_str($UrlArr[‘query’],$ParamArr);

unset($ParamArr[$this->PageName]);

}

$ParamStr=http_build_query($ParamArr);

$Path=empty($UrlArr[‘path’])?'’:$UrlArr[‘path’];

$Mark=empty($ParamStr)?'’:$ParamStr.’&’;

$NewUrlStr=$Url.$Path.’?’.$Mark.$this->PageName.’=’;

$this->CurrentUrl=$NewUrlStr;

}

//设置首页

private function SetHomePage()

{

$Str='<a href=”‘.$this->CurrentUrl.’1″>’.$this->HomeName.'</a>’;

return $Str;

}

//设置上一页

private function SetPrevName()

{

$CurrentPage=1;

if($this->CurrentPageValue>1)

{

$CurrentPage=$this->CurrentPageValue-1;

}

$Str='<a href=”‘.$this->CurrentUrl.$CurrentPage.'”>’.$this->PrevName.'</a>’;

return $Str;

}

//设置下一页

private function SetNextName()

{

$CurrentPage=$this->AllPageCount;

if($this->CurrentPageValue<$CurrentPage)

{

$CurrentPage=$this->CurrentPageValue+1;

}

if(empty($CurrentPage))

{

$CurrentPage=1;

}

$Str='<a href=”‘.$this->CurrentUrl.$CurrentPage.'”>’.$this->NextName.'</a>’;

return $Str;

}

//设置末页

private function GetEndName()

{

$CurrentPage=$this->AllPageCount;

if(empty($CurrentPage))

{

$CurrentPage=1;

}

$Str='<a href=”‘.$this->CurrentUrl.$CurrentPage.'”>’.$this->EndName.'</a>’;

return $Str;

}

//设置索引连接

private function SetLink()

{

$Excursion=floor($this->IndexShowCount/2);

$Min=1;

$Max=$this->IndexShowCount;

if($this->CurrentPageValue>$Excursion)

{

$Min=$this->CurrentPageValue-$Excursion;

$Max=$this->CurrentPageValue+$Excursion;

}

if($this->CurrentPageValue+$Excursion>$this->AllPageCount)

{

$Min=$this->AllPageCount-$this->IndexShowCount+1;

if($this->AllPageCount<$this->IndexShowCount)

{

$Min=1;

}

$Max=$this->AllPageCount;

}

$Str='’;

for($i=$Min;$i<=$Max;$i++)

{

$Class='’;

if(!empty($this->CurrentSeatClass) && $this->CurrentPageValue==$i)

{

$Class=’ class=”‘.$this->CurrentSeatClass.'”‘;

}

$Str.='<a href=”‘.$this->CurrentUrl.$i.'”‘.$Class.’>’.$i.'</a>’;

}

return $Str;

}

}

?>

转载请注明:PHP学习 » PHP翻页类 调用实例

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址