《PHP翻页类 调用实例》要点:
本文介绍了PHP翻页类 调用实例,希望对您有用。如果有疑问,可以联系我们。
《PHP翻页类 调用实例》是否对您有启发,欢迎查看更多与《PHP翻页类 调用实例》相关教程,学精学透。维易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;
}
}
必修>